Pranith06 commited on
Commit
2dd1d08
·
verified ·
1 Parent(s): 8543973

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -49
app.py CHANGED
@@ -1,57 +1,32 @@
1
- import os
2
- import requests
3
  import gradio as gr
 
4
 
5
- # Hugging Face Inference API
6
- API_URL = "https://api-inference.huggingface.co/models/sunnwei/ClinicalBERT-finetuned-disease"
7
- headers = {"Authorization": f"Bearer {os.environ['HF_API_TOKEN']}"}
8
 
9
- # Disease data
10
- disease_symptoms = {
11
- "flu": ["fever", "cough", "sore throat", "fatigue"],
12
- "diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
13
- "covid-19": ["fever", "dry cough", "loss of taste or smell", "difficulty breathing"]
14
- }
15
 
16
- medical_guidelines = {
17
- "flu": "Drink warm fluids, rest, and take antiviral medication if severe.",
18
- "diabetes": "Monitor blood sugar, maintain a healthy diet, and exercise.",
19
- "covid-19": "Isolate, monitor oxygen levels, and seek medical attention if necessary."
20
- }
21
 
22
- def identify_disease(symptoms):
23
- try:
24
- response = requests.post(API_URL, headers=headers, json={"inputs": symptoms})
25
- response.raise_for_status()
26
- prediction = response.json()
27
- return prediction[0]["label"]
28
- except Exception:
29
- return "Error contacting API."
30
 
31
- def get_symptoms(disease):
32
- return disease_symptoms.get(disease.lower(), "No data available.")
 
 
 
33
 
34
- def provide_assistance(disease):
35
- return medical_guidelines.get(disease.lower(), "Consult a healthcare professional.")
 
 
 
36
 
37
- def ai_health_assistant(user_input):
38
- if not user_input.strip():
39
- return "Please enter your symptoms."
40
-
41
- if user_input.lower() in disease_symptoms:
42
- symptoms = get_symptoms(user_input)
43
- advice = provide_assistance(user_input)
44
- return f"✅ Known Disease: **{user_input.title()}**\n\nSymptoms: {symptoms}\nAdvice: {advice}"
45
- else:
46
- disease = identify_disease(user_input)
47
- symptoms = get_symptoms(disease)
48
- advice = provide_assistance(disease)
49
- return f"🧠 Predicted Disease: **{disease}**\n\nSymptoms: {symptoms}\nAdvice: {advice}"
50
-
51
- # Launch Gradio interface
52
- gr.Interface(fn=ai_health_assistant,
53
- inputs="text",
54
- outputs="markdown",
55
- title="🩺 AI Health Assistant",
56
- description="Enter symptoms or disease name to get predictions and medical guidance.",
57
- theme="soft").launch()
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # Load the model from Hugging Face Hub
5
+ generator = pipeline("text-generation", model="ibm-granite/granite-3.3-2b-instruct", device_map="auto")
 
6
 
7
+ def predict_disease(symptoms):
8
+ prompt = f"User has the following symptoms: {symptoms}. What is the most probable disease?"
9
+ result = generator(prompt, max_new_tokens=100, do_sample=True)[0]["generated_text"]
10
+ return result.split(prompt)[-1].strip()
 
 
11
 
12
+ def home_remedy(disease):
13
+ prompt = f"Suggest a natural home remedy for the disease: {disease}."
14
+ result = generator(prompt, max_new_tokens=100, do_sample=True)[0]["generated_text"]
15
+ return result.split(prompt)[-1].strip()
 
16
 
17
+ with gr.Blocks() as demo:
18
+ gr.Markdown("## 🧠 HealthAI")
 
 
 
 
 
 
19
 
20
+ with gr.Tab("🔍 Symptoms Identifier"):
21
+ symptoms_input = gr.Textbox(label="Enter your symptoms")
22
+ symptoms_output = gr.Textbox(label="Predicted Disease")
23
+ symptoms_btn = gr.Button("Predict Disease")
24
+ symptoms_btn.click(fn=predict_disease, inputs=symptoms_input, outputs=symptoms_output)
25
 
26
+ with gr.Tab("🌿 Home Remedies"):
27
+ disease_input = gr.Textbox(label="Enter your disease")
28
+ remedy_output = gr.Textbox(label="Suggested Remedy")
29
+ remedy_btn = gr.Button("Get Remedy")
30
+ remedy_btn.click(fn=home_remedy, inputs=disease_input, outputs=remedy_output)
31
 
32
+ demo.launch()