File size: 2,581 Bytes
bed3e43 9d31a93 51eaf59 9d31a93 51eaf59 9d31a93 51eaf59 bed3e43 9d31a93 bed3e43 9d31a93 d4d4a40 bed3e43 9d31a93 bed3e43 9d31a93 bed3e43 9d31a93 bed3e43 9d31a93 bed3e43 9d31a93 bed3e43 9d31a93 bed3e43 9d31a93 bed3e43 9d31a93 bed3e43 51eaf59 bed3e43 9d31a93 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import os
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# β
IBM Granite model setup
model_id = "ibm-granite/granite-3.3-2b-instruct"
token = os.getenv("HF_TOKEN") # Ensure your Hugging Face token is set in the environment
# β
Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
model = AutoModelForCausalLM.from_pretrained(
model_id,
token=token,
device_map="auto",
torch_dtype=torch.float32
)
# β
Query function
def query_granite(prompt):
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=100)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
# β
Gradio UI using Tabs (instead of Pages)
with gr.Blocks() as demo:
gr.Markdown("# π₯ Welcome to HealthAI")
gr.Markdown("Your intelligent healthcare assistant.")
with gr.Tab("π©Ί Symptoms"):
def identify(symptom):
return query_granite(f"What illness could cause: {symptom}?")
symptom = gr.Textbox(label="Enter your symptom")
output = gr.Textbox(label="AI Diagnosis")
btn = gr.Button("Analyze")
btn.click(identify, inputs=symptom, outputs=output)
with gr.Tab("πΏ Remedies"):
def get_remedies(issue):
return query_granite(f"What are home remedies for {issue}?")
issue = gr.Textbox(label="What are you suffering from?")
remedy_output = gr.Textbox(label="Suggested Remedy")
remedy_btn = gr.Button("Suggest")
remedy_btn.click(get_remedies, inputs=issue, outputs=remedy_output)
with gr.Tab("π₯ Diet"):
def suggest(goal):
return query_granite(f"Suggest a diet for: {goal}")
goal = gr.Textbox(label="Your health goal")
diet_output = gr.Textbox(label="Diet Plan")
diet_btn = gr.Button("Get Plan")
diet_btn.click(suggest, inputs=goal, outputs=diet_output)
with gr.Tab("π§ Mental Wellness"):
def tip(topic):
return query_granite(f"Mental health advice about: {topic}")
topic = gr.Textbox(label="Enter mental health topic")
tip_output = gr.Textbox(label="Wellness Tip")
tip_btn = gr.Button("Get Tip")
tip_btn.click(tip, inputs=topic, outputs=tip_output)
with gr.Tab("β FAQs"):
gr.Markdown("### β FAQs")
gr.Markdown("**Q1:** What is HealthAI?")
gr.Markdown("**A:** It's an AI assistant to help with health-related queries using IBM Granite 3.3-2B.")
demo.launch()
|