-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (53 loc) · 1.9 KB
/
Copy pathmain.py
File metadata and controls
67 lines (53 loc) · 1.9 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, EmailStr, Field
from fastapi.middleware.cors import CORSMiddleware
from uuid import uuid4
from client import chatbot, chatbot2
from summarizer import summarize_chat
from mailer import send_email
app = FastAPI()
# Allow all CORS for testing
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# In-memory store for chat sessions
chat_sessions = {}
class ChatRequest(BaseModel):
session_id: str
user_message: str
class ChatResponse(BaseModel):
reply: str
session_id: str
class SubmitReportRequest(BaseModel):
session_id: str
UserEMail: EmailStr = Field(..., alias="userEmail")
sendEmail: bool
class Config:
validate_by_name = True
@app.post("/chat", response_model=ChatResponse)
async def chat_endpoint(request: ChatRequest):
session_id = request.session_id
if not session_id or session_id not in chat_sessions:
session_id = str(uuid4())
chat_sessions[session_id] = []
user_message = request.user_message
chat_sessions[session_id].append({"role": "user", "content": user_message})
try:
reply = chatbot(chat_sessions[session_id])
chat_sessions[session_id].append({"role": "assistant", "content": reply})
return {"reply": reply, "session_id": session_id}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/submit-report")
async def submit_report(report: SubmitReportRequest):
session_id = report.session_id
if session_id not in chat_sessions:
raise HTTPException(status_code=404, detail="No conversation found")
messages = chat_sessions[session_id]
summary = summarize_chat(messages)
if report.sendEmail:
await send_email(summary, report.UserEMail)
return {"summary": summary, "sent": report.sendEmail}