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