|
import gradio as gr |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
import torch |
|
|
|
|
|
model_name = "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
|
|
|
if tokenizer.pad_token is None: |
|
tokenizer.pad_token = tokenizer.eos_token |
|
|
|
def skin_disease_assistant(user_input): |
|
|
|
prompt = f"""You are a medical AI assistant specializing in skin diseases. Provide helpful, accurate information about skin conditions based on the following query. |
|
|
|
Query: {user_input} |
|
|
|
Response:""" |
|
|
|
|
|
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512) |
|
|
|
with torch.no_grad(): |
|
outputs = model.generate( |
|
inputs.input_ids, |
|
max_new_tokens=200, |
|
temperature=0.7, |
|
do_sample=True, |
|
pad_token_id=tokenizer.eos_token_id, |
|
eos_token_id=tokenizer.eos_token_id |
|
) |
|
|
|
|
|
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
response = full_response.split("Response:")[-1].strip() |
|
|
|
return response |
|
|
|
|
|
demo = gr.Interface( |
|
fn=skin_disease_assistant, |
|
inputs=gr.Textbox(label="Ask about skin conditions", placeholder="e.g., What are the symptoms of eczema?"), |
|
outputs=gr.Textbox(label="AI Response"), |
|
title="🏥 Skin Disease AI Assistant", |
|
description="An AI assistant to help with skin disease questions. For educational purposes only - consult medical professionals for actual diagnosis.", |
|
examples=[ |
|
"What are the symptoms of psoriasis?", |
|
"How to treat acne?", |
|
"What causes eczema flare-ups?", |
|
"Difference between melanoma and mole?" |
|
] |
|
) |
|
|
|
demo.launch() |
|
|