1
+ from fastapi import FastAPI, Body
2
+ from pydantic import BaseModel
3
+ import sqlite3
4
+ from .pages.DarkGPT import get_bot_response, get_model, get_provider, display_model_mapping
5
+ from .pages.summarize import summarize_text, analyze_text
6
+
7
+ app = FastAPI()
8
+
9
+ # Create a connection to the database
10
+ conn = sqlite3.connect('chat_history.db')
11
+ c = conn.cursor()
12
+
13
+ # Create a class to represent the chat request
14
+ class ChatRequest(BaseModel):
15
+ user_input: str
16
+ model_name: str
17
+
18
+ # Create a class to represent the chat response
19
+ class ChatResponse(BaseModel):
20
+ bot_response: str
21
+
22
+ # Create a class to represent the summary request
23
+ class SummaryRequest(BaseModel):
24
+ input_text: str
25
+
26
+ # Create a class to represent the summary response
27
+ class SummaryResponse(BaseModel):
28
+ summarized_text: str
29
+
30
+ # Create a class to represent the analysis request
31
+ class AnalysisRequest(BaseModel):
32
+ input_text: str
33
+
34
+ # Create a class to represent the analysis response
35
+ class AnalysisResponse(BaseModel):
36
+ reading_time: str
37
+ text_complexity: str
38
+ lexical_richness: str
39
+ num_sentences: str
40
+
41
+ # Create an endpoint for the chatbot
42
+ @app.post("/chat", response_model=ChatResponse)
43
+ async def chat(request: ChatRequest = Body(...)):
44
+ try:
45
+ internal_model = get_model(request.model_name)
46
+ provider_name = get_provider(internal_model)
47
+ bot_response = get_bot_response(request.user_input, internal_model, provider_name)
48
+ # Save the chat history to the database
49
+ c.execute("INSERT INTO chat_history VALUES (?, ?, ?)", (1, "user", request.user_input))
50
+ c.execute("INSERT INTO chat_history VALUES (?, ?, ?)", (1, "bot", bot_response))
51
+ conn.commit()
52
+ return {"bot_response": bot_response}
53
+ except Exception as e:
54
+ return {"error": str(e)}
55
+
56
+ # Create an endpoint for the summarization
57
+ @app.post("/summarize", response_model=SummaryResponse)
58
+ async def summarize(request: SummaryRequest = Body(...)):
59
+ try:
60
+ summarized_text = summarize_text(request.input_text)
61
+ return {"summarized_text": summarized_text}
62
+ except Exception as e:
63
+ return {"error": str(e)}
64
+
65
+ # Create an endpoint for the analysis
66
+ @app.post("/analyze", response_model=AnalysisResponse)
67
+ async def analyze(request: AnalysisRequest = Body(...)):
68
+ try:
69
+ analysis_results = analyze_text(request.input_text)
70
+ return {
71
+ "reading_time": analysis_results["reading_time"],
72
+ "text_complexity": analysis_results["text_complexity"],
73
+ "lexical_richness": analysis_results["lexical_richness"],
74
+ "num_sentences": analysis_results["num_sentences"]
75
+ }
76
+ except Exception as e:
77
+ return {"error": str(e)}
0 commit comments