Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,63 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
from transformers import pipeline
|
3 |
-
|
4 |
-
|
5 |
-
app=FastAPI()
|
6 |
-
|
7 |
-
# Initialize the text generation pipeline
|
8 |
-
pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Query
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Create a new FastAPI app instance
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
# Initialize the text generation pipeline
|
8 |
+
pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
def generate_advice(generated_text):
|
13 |
+
# Placeholder function for generating actionable advice based on generated text
|
14 |
+
# Replace with actual logic or integrate with medical knowledge base for accurate advice
|
15 |
+
# Example keywords for different health issues
|
16 |
+
keywords_pain = ["stomach ache", "headache", "pain"]
|
17 |
+
keywords_blurred_vision = ["blurred vision", "vision issues", "eye problems"]
|
18 |
+
keywords_mental_health = ["anxiety", "depression", "stress"]
|
19 |
+
|
20 |
+
advice = []
|
21 |
+
|
22 |
+
# Check for specific health issues in the generated text
|
23 |
+
for keyword in keywords_pain:
|
24 |
+
if keyword in generated_text.lower():
|
25 |
+
advice.append("Consider taking over-the-counter pain relief medication. If the pain persists or worsens, consult a doctor.")
|
26 |
+
|
27 |
+
for keyword in keywords_blurred_vision:
|
28 |
+
if keyword in generated_text.lower():
|
29 |
+
advice.append("Schedule an appointment with an ophthalmologist for a thorough eye examination.")
|
30 |
+
|
31 |
+
for keyword in keywords_mental_health:
|
32 |
+
if keyword in generated_text.lower():
|
33 |
+
advice.append("Practice mindfulness techniques, consider talking to a therapist, or consult a mental health professional for support.")
|
34 |
+
|
35 |
+
# If no specific advice was generated, provide a general recommendation
|
36 |
+
if not advice:
|
37 |
+
advice.append("Please consult a healthcare professional for a proper diagnosis and treatment.")
|
38 |
+
|
39 |
+
# Add a general prompt suggestion similar to ChatGPT
|
40 |
+
advice.append("Feel free to ask more about any specific concerns or questions you have.")
|
41 |
+
|
42 |
+
# Return the advice as a formatted string or list
|
43 |
+
return advice
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
@app.get("/")
|
48 |
+
def home():
|
49 |
+
return {"message": "Hello World"}
|
50 |
+
|
51 |
+
# Define a function to handle the GET request at `/generate`
|
52 |
+
@app.get("/generate")
|
53 |
+
def generate(text: str = Query(..., title="Input Text", description="Describe your health issue here")):
|
54 |
+
# Use the pipeline to generate text based on the input text
|
55 |
+
output = pipe(text)
|
56 |
+
# Extract the generated text from the pipeline output
|
57 |
+
generated_text = output[0]['generated_text']
|
58 |
+
# Enhance the response with actionable advice based on the generated text
|
59 |
+
advice = generate_advice(generated_text)
|
60 |
+
# Return the input text, generated text, and advice as a JSON response
|
61 |
+
return {"input": text, "generated_output": generated_text, "advice": advice}
|
62 |
+
|
63 |
+
|