Kalyangotimothy Amp commited on
Commit
dcc0018
·
1 Parent(s): c5c5059

Fix gradio app to use actual TinyLlama model for skin disease assistance

Browse files

Co-authored-by: Amp <[email protected]>
Amp-Thread-ID: https://ampcode.com/threads/T-31dcde95-a69d-4276-aff8-3096bca9d588

Files changed (1) hide show
  1. gradio-app.py +53 -3
gradio-app.py CHANGED
@@ -1,7 +1,57 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
 
5
+ # Load the model and tokenizer
6
+ model_name = "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
+
10
+ # Add pad token if it doesn't exist
11
+ if tokenizer.pad_token is None:
12
+ tokenizer.pad_token = tokenizer.eos_token
13
+
14
+ def skin_disease_assistant(user_input):
15
+ # Create a medical-focused prompt
16
+ prompt = f"""You are a medical AI assistant specializing in skin diseases. Provide helpful, accurate information about skin conditions based on the following query.
17
+
18
+ Query: {user_input}
19
+
20
+ Response:"""
21
+
22
+ # Tokenize and generate response
23
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
24
+
25
+ with torch.no_grad():
26
+ outputs = model.generate(
27
+ inputs.input_ids,
28
+ max_new_tokens=200,
29
+ temperature=0.7,
30
+ do_sample=True,
31
+ pad_token_id=tokenizer.eos_token_id,
32
+ eos_token_id=tokenizer.eos_token_id
33
+ )
34
+
35
+ # Decode the response
36
+ full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
37
+ # Extract only the generated part (after "Response:")
38
+ response = full_response.split("Response:")[-1].strip()
39
+
40
+ return response
41
+
42
+ # Create Gradio interface
43
+ demo = gr.Interface(
44
+ fn=skin_disease_assistant,
45
+ inputs=gr.Textbox(label="Ask about skin conditions", placeholder="e.g., What are the symptoms of eczema?"),
46
+ outputs=gr.Textbox(label="AI Response"),
47
+ title="🏥 Skin Disease AI Assistant",
48
+ description="An AI assistant to help with skin disease questions. For educational purposes only - consult medical professionals for actual diagnosis.",
49
+ examples=[
50
+ "What are the symptoms of psoriasis?",
51
+ "How to treat acne?",
52
+ "What causes eczema flare-ups?",
53
+ "Difference between melanoma and mole?"
54
+ ]
55
+ )
56
 
 
57
  demo.launch()