Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,9 @@ from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoM
|
|
7 |
from peft import PeftModel, PeftConfig
|
8 |
import torch
|
9 |
import gradio as gr
|
|
|
|
|
|
|
10 |
|
11 |
# Functions to Wrap the Prompt Correctly
|
12 |
def wrap_text(text, width=90):
|
@@ -14,7 +17,6 @@ def wrap_text(text, width=90):
|
|
14 |
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
15 |
wrapped_text = '\n'.join(wrapped_lines)
|
16 |
return wrapped_text
|
17 |
-
|
18 |
def multimodal_prompt(user_input, system_prompt="You are an expert medical analyst:"):
|
19 |
"""
|
20 |
Generates text using a large language model, given a user input and a system prompt.
|
@@ -25,7 +27,7 @@ def multimodal_prompt(user_input, system_prompt="You are an expert medical analy
|
|
25 |
A string containing the generated text.
|
26 |
"""
|
27 |
# Combine user input and system prompt
|
28 |
-
formatted_input = f"
|
29 |
|
30 |
# Encode the input text
|
31 |
encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
|
@@ -53,12 +55,12 @@ def multimodal_prompt(user_input, system_prompt="You are an expert medical analy
|
|
53 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
54 |
|
55 |
# Use the base model's ID
|
56 |
-
base_model_id = "
|
57 |
-
model_directory = "Tonic/
|
58 |
|
59 |
# Instantiate the Tokenizer
|
60 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
61 |
-
# tokenizer = AutoTokenizer.from_pretrained("Tonic/
|
62 |
tokenizer.pad_token = tokenizer.eos_token
|
63 |
tokenizer.padding_side = 'left'
|
64 |
|
@@ -69,9 +71,9 @@ tokenizer.padding_side = 'left'
|
|
69 |
#peft_model = AutoModelForCausalLM.from_pretrained(base_model_id, config=model_config)
|
70 |
|
71 |
# Load the PEFT model
|
72 |
-
peft_config = PeftConfig.from_pretrained("Tonic/
|
73 |
-
peft_model = MistralForCausalLM.from_pretrained("
|
74 |
-
peft_model = PeftModel.from_pretrained(peft_model, "Tonic/
|
75 |
|
76 |
class ChatBot:
|
77 |
def __init__(self):
|
@@ -79,7 +81,7 @@ class ChatBot:
|
|
79 |
|
80 |
def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
|
81 |
# Combine user input and system prompt
|
82 |
-
formatted_input = f"
|
83 |
|
84 |
# Encode user input
|
85 |
user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
|
@@ -91,7 +93,7 @@ class ChatBot:
|
|
91 |
chat_history_ids = user_input_ids
|
92 |
|
93 |
# Generate a response using the PEFT model
|
94 |
-
response = peft_model.generate(input_ids=chat_history_ids, max_length=
|
95 |
|
96 |
# Update chat history
|
97 |
self.history = chat_history_ids
|
@@ -102,8 +104,8 @@ class ChatBot:
|
|
102 |
|
103 |
bot = ChatBot()
|
104 |
|
105 |
-
title = "👋🏻Welcome to Tonic's
|
106 |
-
description = "You can use this Space to test out the current model (
|
107 |
examples = [["What is the proper treatment for buccal herpes?", "Please provide information on the most effective antiviral medications and home remedies for treating buccal herpes."]]
|
108 |
|
109 |
iface = gr.Interface(
|
|
|
7 |
from peft import PeftModel, PeftConfig
|
8 |
import torch
|
9 |
import gradio as gr
|
10 |
+
import os
|
11 |
+
|
12 |
+
hf_token = os.environ.get('HUGGINGFACE_TOKEN')
|
13 |
|
14 |
# Functions to Wrap the Prompt Correctly
|
15 |
def wrap_text(text, width=90):
|
|
|
17 |
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
18 |
wrapped_text = '\n'.join(wrapped_lines)
|
19 |
return wrapped_text
|
|
|
20 |
def multimodal_prompt(user_input, system_prompt="You are an expert medical analyst:"):
|
21 |
"""
|
22 |
Generates text using a large language model, given a user input and a system prompt.
|
|
|
27 |
A string containing the generated text.
|
28 |
"""
|
29 |
# Combine user input and system prompt
|
30 |
+
formatted_input = f"[INSTRUCTION]{system_prompt} {user_input}"
|
31 |
|
32 |
# Encode the input text
|
33 |
encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
|
|
|
55 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
56 |
|
57 |
# Use the base model's ID
|
58 |
+
base_model_id = "stabilityai/stablelm-3b-4e1t"
|
59 |
+
model_directory = "Tonic/stablemed"
|
60 |
|
61 |
# Instantiate the Tokenizer
|
62 |
+
tokenizer = AutoTokenizer.from_pretrained("stabilityai/stablelm-3b-4e1t", trust_remote_code=True, padding_side="left")
|
63 |
+
# tokenizer = AutoTokenizer.from_pretrained("Tonic/stablemed", trust_remote_code=True, padding_side="left")
|
64 |
tokenizer.pad_token = tokenizer.eos_token
|
65 |
tokenizer.padding_side = 'left'
|
66 |
|
|
|
71 |
#peft_model = AutoModelForCausalLM.from_pretrained(base_model_id, config=model_config)
|
72 |
|
73 |
# Load the PEFT model
|
74 |
+
peft_config = PeftConfig.from_pretrained("Tonic/stablemed", token=hf_token)
|
75 |
+
peft_model = MistralForCausalLM.from_pretrained("stabilityai/stablelm-3b-4e1t", trust_remote_code=True)
|
76 |
+
peft_model = PeftModel.from_pretrained(peft_model, "Tonic/stablemed", token=hf_token)
|
77 |
|
78 |
class ChatBot:
|
79 |
def __init__(self):
|
|
|
81 |
|
82 |
def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
|
83 |
# Combine user input and system prompt
|
84 |
+
formatted_input = f"[INSTRUCTION:]{system_prompt}[QUESTION:] {user_input}"
|
85 |
|
86 |
# Encode user input
|
87 |
user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
|
|
|
93 |
chat_history_ids = user_input_ids
|
94 |
|
95 |
# Generate a response using the PEFT model
|
96 |
+
response = peft_model.generate(input_ids=chat_history_ids, max_length=400, pad_token_id=tokenizer.eos_token_id)
|
97 |
|
98 |
# Update chat history
|
99 |
self.history = chat_history_ids
|
|
|
104 |
|
105 |
bot = ChatBot()
|
106 |
|
107 |
+
title = "👋🏻Welcome to Tonic's StableMed Chat🚀"
|
108 |
+
description = "You can use this Space to test out the current model [StableMed](https://huggingface.co/Tonic/stablemed) or You can also use 😷StableMed⚕️ on your own data & in your own way by cloning this space. 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/StableMed_Chat?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3> #### Join us : 🌟TeamTonic�� is always making cool demos! Join our active builder's🛠️community on 👻Discord: [Discord](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [PolyGPT](https://github.com/tonic-ai/polygpt-alpha)" "
|
109 |
examples = [["What is the proper treatment for buccal herpes?", "Please provide information on the most effective antiviral medications and home remedies for treating buccal herpes."]]
|
110 |
|
111 |
iface = gr.Interface(
|