zeeshanali01 commited on
Commit
5723615
·
verified ·
1 Parent(s): 7d2732a

Rename main.py to app.py

Browse files
Files changed (1) hide show
  1. main.py → app.py +71 -73
main.py → app.py RENAMED
@@ -1,73 +1,71 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
- from dotenv import load_dotenv
4
- import os
5
- import google.generativeai as genai
6
- import joblib
7
-
8
- # Load environment variables
9
- load_dotenv()
10
- genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
11
-
12
- # Load the machine learning model
13
- try:
14
- model = joblib.load('./movie_review_classifier.joblib')
15
- except Exception as e:
16
- raise ImportError(f"Failed to load model: {e}")
17
-
18
- app = FastAPI()
19
-
20
- # Define models for requests
21
- class QueryRequest(BaseModel):
22
- question: str
23
-
24
- class Review(BaseModel):
25
- text: str
26
-
27
- # Initialize the Gemini chat model
28
- gemini_model = genai.GenerativeModel("gemini-pro")
29
- chat = gemini_model.start_chat(history=[])
30
-
31
- mental_health_prompt = """
32
- You are an expert in providing mental health support. When a user describes their mental health issues,
33
- you should provide relevant articles or blog posts to assist them.
34
- """
35
-
36
- # Gemini response function
37
- def get_gemini_response(question, prompt):
38
- response = chat.send_message(f"{prompt} {question}", stream=True)
39
- return [chunk.text for chunk in response]
40
-
41
- # Function to retrieve articles from a database or external source
42
- def get_articles(query):
43
- return [
44
- {"title": "Understanding Anxiety", "url": "https://newsinhealth.nih.gov/2016/03/understanding-anxiety-disorders", "summary": "A comprehensive guide on anxiety disorders."},
45
- {"title": "Coping with Depression", "url": "https://www.helpguide.org/articles/depression/coping-with-depression.htm", "summary": "Effective strategies for dealing with depression."}
46
- ]
47
-
48
- # Mental health support endpoint
49
- @app.post("/rag")
50
- async def mental_health_support(request: QueryRequest):
51
- try:
52
- responses = get_gemini_response(request.question, mental_health_prompt)
53
- articles = get_articles(request.question)
54
- result = {"responses": responses, "articles": articles}
55
- return result
56
- except Exception as e:
57
- raise HTTPException(status_code=500, detail=str(e))
58
-
59
- # Classification endpoint
60
- @app.post("/classification")
61
- async def classify_review(review: Review):
62
- try:
63
- prediction = model.predict([review.text])
64
- return {"predicted_sentiment": prediction[0]}
65
- except Exception as e:
66
- raise HTTPException(status_code=500, detail=str(e))
67
-
68
- # Main function to run the server
69
- if __name__ == "__main__":
70
- import uvicorn
71
- uvicorn.run(app, host="0.0.0.0", port=8000)
72
-
73
-
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ from dotenv import load_dotenv
4
+ import os
5
+ import google.generativeai as genai
6
+ import joblib
7
+
8
+ # Load environment variables
9
+ load_dotenv()
10
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
11
+
12
+ # Load the machine learning model
13
+ try:
14
+ model = joblib.load('./movie_review_classifier.joblib')
15
+ except Exception as e:
16
+ raise ImportError(f"Failed to load model: {e}")
17
+
18
+ app = FastAPI()
19
+
20
+ # Define models for requests
21
+ class QueryRequest(BaseModel):
22
+ question: str
23
+
24
+ class Review(BaseModel):
25
+ text: str
26
+
27
+ # Initialize the Gemini chat model
28
+ gemini_model = genai.GenerativeModel("gemini-pro")
29
+ chat = gemini_model.start_chat(history=[])
30
+
31
+ mental_health_prompt = """
32
+ You are an expert in providing mental health support. When a user describes their mental health issues,
33
+ you should provide relevant articles or blog posts to assist them.
34
+ """
35
+
36
+ # Gemini response function
37
+ def get_gemini_response(question, prompt):
38
+ response = chat.send_message(f"{prompt} {question}", stream=True)
39
+ return [chunk.text for chunk in response]
40
+
41
+ # Function to retrieve articles from a database or external source
42
+ def get_articles(query):
43
+ return [
44
+ {"title": "Understanding Anxiety", "url": "https://newsinhealth.nih.gov/2016/03/understanding-anxiety-disorders", "summary": "A comprehensive guide on anxiety disorders."},
45
+ {"title": "Coping with Depression", "url": "https://www.helpguide.org/articles/depression/coping-with-depression.htm", "summary": "Effective strategies for dealing with depression."}
46
+ ]
47
+
48
+ # Mental health support endpoint
49
+ @app.post("/rag")
50
+ async def mental_health_support(request: QueryRequest):
51
+ try:
52
+ responses = get_gemini_response(request.question, mental_health_prompt)
53
+ articles = get_articles(request.question)
54
+ result = {"responses": responses, "articles": articles}
55
+ return result
56
+ except Exception as e:
57
+ raise HTTPException(status_code=500, detail=str(e))
58
+
59
+ # Classification endpoint
60
+ @app.post("/classification")
61
+ async def classify_review(review: Review):
62
+ try:
63
+ prediction = model.predict([review.text])
64
+ return {"predicted_sentiment": prediction[0]}
65
+ except Exception as e:
66
+ raise HTTPException(status_code=500, detail=str(e))
67
+
68
+ # Main function to run the server
69
+ if __name__ == "__main__":
70
+ import uvicorn
71
+ uvicorn.run(app, host="0.0.0.0", port=7860)