-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (39 loc) · 1.27 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def generate_summary(text):
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=[
{"role": "system", "content": "You are a helpful, pattern-following assistant that summarizes historical content into easy to understand, bite-sized pieces."},
{"role": "user", "content": text}
],
max_tokens=150,
temperature=0.7,
n=1,
stop=None
)
summary = response.choices[0].message.content
cleaned_summary = summary.replace('\n', '').replace('\n\n', ' ')
return cleaned_summary
@app.get("/")
def home():
return {"message": "Welcome to the Text Summarizer API"}
@app.post("/summarize/")
def summarize_text(data: dict):
text = data.get("text", "")
summary = generate_summary(text)
return {"summary": summary}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)