Upload app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,11 @@ import gradio as gr
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# β
|
| 7 |
model_id = "ibm-granite/granite-3.3-2b-instruct"
|
| 8 |
-
token = os.getenv("HF_TOKEN") #
|
| 9 |
|
| 10 |
-
# β
Load model and tokenizer
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
model_id,
|
|
@@ -16,78 +16,52 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 16 |
torch_dtype=torch.float32
|
| 17 |
)
|
| 18 |
|
|
|
|
| 19 |
def query_granite(prompt):
|
| 20 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 21 |
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 22 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
|
| 24 |
-
# Pages
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
gr.Markdown("Your intelligent healthcare assistant using IBM Granite 3.3-2B Instruct.")
|
| 29 |
-
return demo
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
with gr.Blocks() as demo:
|
| 35 |
-
gr.Markdown("## π©Ί Symptom Identifier")
|
| 36 |
symptom = gr.Textbox(label="Enter your symptom")
|
| 37 |
output = gr.Textbox(label="AI Diagnosis")
|
| 38 |
btn = gr.Button("Analyze")
|
| 39 |
btn.click(identify, inputs=symptom, outputs=output)
|
| 40 |
-
return demo
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
with gr.Blocks() as demo:
|
| 46 |
-
gr.Markdown("## πΏ Home Remedies")
|
| 47 |
issue = gr.Textbox(label="What are you suffering from?")
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
return demo
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
with gr.Blocks() as demo:
|
| 57 |
-
gr.Markdown("## π₯ Diet Suggestions")
|
| 58 |
goal = gr.Textbox(label="Your health goal")
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
return demo
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
with gr.Blocks() as demo:
|
| 68 |
-
gr.Markdown("## π§ Mental Wellness")
|
| 69 |
topic = gr.Textbox(label="Enter mental health topic")
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
return demo
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
gr.Markdown("## β FAQs")
|
| 78 |
gr.Markdown("**Q1:** What is HealthAI?")
|
| 79 |
gr.Markdown("**A:** It's an AI assistant to help with health-related queries using IBM Granite 3.3-2B.")
|
| 80 |
-
return demo
|
| 81 |
|
| 82 |
-
|
| 83 |
-
pages = [
|
| 84 |
-
gr.Page(title="π Home", path="/", block=home),
|
| 85 |
-
gr.Page(title="π©Ί Symptoms", path="/symptoms", block=symptoms_app),
|
| 86 |
-
gr.Page(title="πΏ Remedies", path="/remedies", block=remedies_app),
|
| 87 |
-
gr.Page(title="π₯ Diet", path="/diet", block=diet_app),
|
| 88 |
-
gr.Page(title="π§ Mental Wellness", path="/mental", block=mental_app),
|
| 89 |
-
gr.Page(title="β FAQs", path="/faqs", block=faqs_app),
|
| 90 |
-
]
|
| 91 |
-
|
| 92 |
-
app = gr.App(pages=pages)
|
| 93 |
-
app.launch()
|
|
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# β
IBM Granite model setup
|
| 7 |
model_id = "ibm-granite/granite-3.3-2b-instruct"
|
| 8 |
+
token = os.getenv("HF_TOKEN") # Ensure your Hugging Face token is set in the environment
|
| 9 |
|
| 10 |
+
# β
Load model and tokenizer
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
model_id,
|
|
|
|
| 16 |
torch_dtype=torch.float32
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# β
Query function
|
| 20 |
def query_granite(prompt):
|
| 21 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 22 |
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 23 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
|
| 25 |
+
# β
Gradio UI using Tabs (instead of Pages)
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("# π₯ Welcome to HealthAI")
|
| 28 |
+
gr.Markdown("Your intelligent healthcare assistant using IBM Granite 3.3-2B Instruct.")
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
with gr.Tab("π©Ί Symptoms"):
|
| 31 |
+
def identify(symptom):
|
| 32 |
+
return query_granite(f"What illness could cause: {symptom}?")
|
|
|
|
|
|
|
| 33 |
symptom = gr.Textbox(label="Enter your symptom")
|
| 34 |
output = gr.Textbox(label="AI Diagnosis")
|
| 35 |
btn = gr.Button("Analyze")
|
| 36 |
btn.click(identify, inputs=symptom, outputs=output)
|
|
|
|
| 37 |
|
| 38 |
+
with gr.Tab("πΏ Remedies"):
|
| 39 |
+
def get_remedies(issue):
|
| 40 |
+
return query_granite(f"What are home remedies for {issue}?")
|
|
|
|
|
|
|
| 41 |
issue = gr.Textbox(label="What are you suffering from?")
|
| 42 |
+
remedy_output = gr.Textbox(label="Suggested Remedy")
|
| 43 |
+
remedy_btn = gr.Button("Suggest")
|
| 44 |
+
remedy_btn.click(get_remedies, inputs=issue, outputs=remedy_output)
|
|
|
|
| 45 |
|
| 46 |
+
with gr.Tab("π₯ Diet"):
|
| 47 |
+
def suggest(goal):
|
| 48 |
+
return query_granite(f"Suggest a diet for: {goal}")
|
|
|
|
|
|
|
| 49 |
goal = gr.Textbox(label="Your health goal")
|
| 50 |
+
diet_output = gr.Textbox(label="Diet Plan")
|
| 51 |
+
diet_btn = gr.Button("Get Plan")
|
| 52 |
+
diet_btn.click(suggest, inputs=goal, outputs=diet_output)
|
|
|
|
| 53 |
|
| 54 |
+
with gr.Tab("π§ Mental Wellness"):
|
| 55 |
+
def tip(topic):
|
| 56 |
+
return query_granite(f"Mental health advice about: {topic}")
|
|
|
|
|
|
|
| 57 |
topic = gr.Textbox(label="Enter mental health topic")
|
| 58 |
+
tip_output = gr.Textbox(label="Wellness Tip")
|
| 59 |
+
tip_btn = gr.Button("Get Tip")
|
| 60 |
+
tip_btn.click(tip, inputs=topic, outputs=tip_output)
|
|
|
|
| 61 |
|
| 62 |
+
with gr.Tab("β FAQs"):
|
| 63 |
+
gr.Markdown("### β FAQs")
|
|
|
|
| 64 |
gr.Markdown("**Q1:** What is HealthAI?")
|
| 65 |
gr.Markdown("**A:** It's an AI assistant to help with health-related queries using IBM Granite 3.3-2B.")
|
|
|
|
| 66 |
|
| 67 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|