Pranith06 commited on
Commit
56426a7
·
verified ·
1 Parent(s): 1285447

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -9
app.py CHANGED
@@ -1,30 +1,52 @@
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)
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load the model with accelerate support
5
+ generator = pipeline(
6
+ "text-generation",
7
+ model="ibm-granite/granite-3.3-2b-instruct",
8
+ device_map="auto"
9
+ )
10
 
11
+ # Predict disease from symptoms
12
  def predict_disease(symptoms):
13
  prompt = f"User has the following symptoms: {symptoms}. What is the most probable disease?"
14
+ try:
15
+ output = generator(prompt, max_new_tokens=150, do_sample=True, temperature=0.7)[0]["generated_text"]
16
+ if prompt in output:
17
+ response = output.split(prompt)[-1].strip()
18
+ else:
19
+ response = output.strip()
20
+ return response
21
+ except Exception as e:
22
+ return f"Error: {str(e)}"
23
 
24
+ # Suggest home remedy from disease
25
  def home_remedy(disease):
26
  prompt = f"Suggest a natural home remedy for the disease: {disease}."
27
+ try:
28
+ output = generator(prompt, max_new_tokens=150, do_sample=True, temperature=0.7)[0]["generated_text"]
29
+ if prompt in output:
30
+ response = output.split(prompt)[-1].strip()
31
+ else:
32
+ response = output.strip()
33
+ return response
34
+ except Exception as e:
35
+ return f"Error: {str(e)}"
36
 
37
+ # UI with Gradio
38
  with gr.Blocks() as demo:
39
+ gr.Markdown("# 🧠 HealthAI")
40
+ gr.Markdown("Identify diseases from symptoms and get natural home remedies.")
41
 
42
  with gr.Tab("🔍 Symptoms Identifier"):
43
+ symptoms_input = gr.Textbox(label="Enter your symptoms", placeholder="e.g. fever, cough, sore throat")
44
  symptoms_output = gr.Textbox(label="Predicted Disease")
45
  symptoms_btn = gr.Button("Predict Disease")
46
  symptoms_btn.click(fn=predict_disease, inputs=symptoms_input, outputs=symptoms_output)
47
 
48
  with gr.Tab("🌿 Home Remedies"):
49
+ disease_input = gr.Textbox(label="Enter your disease", placeholder="e.g. Cold, Headache")
50
  remedy_output = gr.Textbox(label="Suggested Remedy")
51
  remedy_btn = gr.Button("Get Remedy")
52
  remedy_btn.click(fn=home_remedy, inputs=disease_input, outputs=remedy_output)