Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,26 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
|
8 |
|
|
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def respond(
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
@@ -25,39 +39,49 @@ def respond(
|
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
yield response
|
41 |
-
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a
|
49 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
],
|
59 |
-
|
|
|
|
|
|
|
60 |
|
|
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
!pip install bitsandbytes accelerate gradio
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
5 |
+
import torch
|
6 |
|
7 |
+
# Define BitsAndBytesConfig
|
8 |
+
bnb_config = BitsAndBytesConfig(load_in_4bit=True,
|
9 |
+
bnb_4bit_quant_type="nf4",
|
10 |
+
bnb_4bit_compute_dtype=torch.float16)
|
11 |
|
12 |
+
# Model name
|
13 |
+
model_name = "ruslanmv/Medical-Llama3-v2"
|
14 |
|
15 |
+
# Load tokenizer and model with BitsAndBytesConfig
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, bnb_config=bnb_config)
|
17 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, config=bnb_config)
|
18 |
+
|
19 |
+
# Ensure model is on the correct device
|
20 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
21 |
+
model.to(device)
|
22 |
+
|
23 |
+
# Define the respond function
|
24 |
def respond(
|
25 |
message,
|
26 |
history: list[tuple[str, str]],
|
|
|
39 |
|
40 |
messages.append({"role": "user", "content": message})
|
41 |
|
42 |
+
# Format the conversation as a single string for the model
|
43 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
44 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True, max_length=1000)
|
45 |
+
|
46 |
+
# Move inputs to device
|
47 |
+
input_ids = inputs['input_ids'].to(device)
|
48 |
+
attention_mask = inputs['attention_mask'].to(device)
|
49 |
+
|
50 |
+
# Generate the response
|
51 |
+
with torch.no_grad():
|
52 |
+
outputs = model.generate(
|
53 |
+
input_ids=input_ids,
|
54 |
+
attention_mask=attention_mask,
|
55 |
+
max_length=max_tokens,
|
56 |
+
temperature=temperature,
|
57 |
+
top_p=top_p,
|
58 |
+
use_cache=True
|
59 |
+
)
|
60 |
+
|
61 |
+
# Extract the response
|
62 |
+
response_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
63 |
+
|
64 |
+
# Remove the prompt and system message from the response
|
65 |
+
response_text = response_text.replace(system_message, '').strip()
|
66 |
+
response_text = response_text.replace(f"Human: {message}\n\nAssistant: ", '').strip()
|
67 |
+
|
68 |
+
return response_text
|
69 |
|
70 |
+
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
71 |
demo = gr.ChatInterface(
|
72 |
respond,
|
73 |
additional_inputs=[
|
74 |
+
gr.Textbox(value="You are a Medical AI Assistant. Please be thorough and provide an informative answer. If you don't know the answer to a specific medical inquiry, advise seeking professional help.", label="System message", lines=3),
|
75 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
76 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
77 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
],
|
79 |
+
title="Medical AI Assistant",
|
80 |
+
description="Give me your symptoms and ask me a health problem. The AI will provide informative answers. If the AI doesn't know the answer, it will advise seeking professional help.",
|
81 |
+
|
82 |
+
examples=[["I have a headache and a fever. What should I do?"], ["What are the symptoms of diabetes?"], ["How can I improve my sleep?"]],
|
83 |
|
84 |
+
)
|
85 |
|
86 |
if __name__ == "__main__":
|
87 |
demo.launch()
|