Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,65 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
for
|
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 |
-
if
|
63 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the tokenizer and quantized model from Hugging Face
|
6 |
+
model_name = "llSourcell/medllama2_7b"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Load model with quantization
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name,
|
11 |
+
load_in_4bit=True,
|
12 |
+
device_map="auto",
|
13 |
+
quantization_config={
|
14 |
+
'bits': 4, # Quantize to 4-bit
|
15 |
+
'group_size': 128
|
16 |
+
})
|
17 |
+
|
18 |
+
def format_history(msg: str, history: list[list[str, str]], system_prompt: str):
|
19 |
+
chat_history = system_prompt
|
20 |
+
for query, response in history:
|
21 |
+
chat_history += f"\nUser: {query}\nAssistant: {response}"
|
22 |
+
chat_history += f"\nUser: {msg}\nAssistant:"
|
23 |
+
return chat_history
|
24 |
+
|
25 |
+
def generate_response(msg: str, history: list[list[str, str]], system_prompt: str):
|
26 |
+
chat_history = format_history(msg, history, system_prompt)
|
27 |
+
|
28 |
+
# Tokenize the input prompt
|
29 |
+
inputs = tokenizer(chat_history, return_tensors="pt").to("cuda")
|
30 |
+
|
31 |
+
# Generate a response using the model
|
32 |
+
outputs = model.generate(inputs["input_ids"], max_length=500, pad_token_id=tokenizer.eos_token_id)
|
33 |
+
|
34 |
+
# Decode the response back to a string
|
35 |
+
response = tokenizer.decode(outputs[:, inputs["input_ids"].shape[-1]:][0], skip_special_tokens=True)
|
36 |
+
|
37 |
+
# Yield the generated response
|
38 |
+
yield response
|
39 |
+
|
40 |
+
# Define the Gradio ChatInterface
|
41 |
+
chatbot = gr.ChatInterface(
|
42 |
+
generate_response,
|
43 |
+
chatbot=gr.Chatbot(
|
44 |
+
height="64vh"
|
45 |
+
),
|
46 |
+
additional_inputs=[
|
47 |
+
gr.Textbox(
|
48 |
+
"Behave as if you are a medical doctor providing answers for patients' clinical questions.",
|
49 |
+
label="System Prompt"
|
50 |
+
)
|
51 |
+
],
|
52 |
+
title="Medical QA Chat",
|
53 |
+
description="Feel free to ask any question to Medllama2 Chatbot.",
|
54 |
+
theme="soft",
|
55 |
+
submit_btn="Send",
|
56 |
+
retry_btn="Regenerate Response",
|
57 |
+
undo_btn="Delete Previous",
|
58 |
+
clear_btn="Clear Chat"
|
59 |
)
|
60 |
|
61 |
+
# Following line is important to queue the messages
|
62 |
+
chatbot.queue()
|
63 |
|
64 |
+
# Enable share = True if you want to create a public link for people to use your application
|
65 |
+
chatbot.launch()
|