Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,100 +1,53 @@
|
|
1 |
-
from transformers import AutoTokenizer,
|
2 |
-
import torch
|
3 |
-
import gradio as gr
|
4 |
-
import random
|
5 |
-
from textwrap import wrap
|
6 |
-
from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, MistralForCausalLM
|
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):
|
16 |
-
lines = text.split('\n')
|
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 |
-
# Combine user input and system prompt
|
23 |
-
formatted_input = f"{user_input}{system_prompt}"
|
24 |
-
|
25 |
-
# Encode the input text
|
26 |
-
encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
|
27 |
-
model_inputs = encodeds.to(device)
|
28 |
-
|
29 |
-
# Generate a response using the model
|
30 |
-
output = model.generate(
|
31 |
-
**model_inputs,
|
32 |
-
max_length=max_length,
|
33 |
-
use_cache=True,
|
34 |
-
early_stopping=True,
|
35 |
-
bos_token_id=model.config.bos_token_id,
|
36 |
-
eos_token_id=model.config.eos_token_id,
|
37 |
-
pad_token_id=model.config.eos_token_id,
|
38 |
-
temperature=0.1,
|
39 |
-
do_sample=True
|
40 |
-
)
|
41 |
-
|
42 |
-
# Decode the response
|
43 |
-
response_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
44 |
-
|
45 |
-
return response_text
|
46 |
-
|
47 |
# Define the device
|
48 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
# Load the PEFT model
|
61 |
-
peft_config = PeftConfig.from_pretrained("Tonic/stablemed", token=hf_token)
|
62 |
-
peft_model = AutoModelForCausalLM.from_pretrained("stabilityai/stablelm-3b-4e1t", token=hf_token, trust_remote_code=True)
|
63 |
-
peft_model = PeftModel.from_pretrained(peft_model, "Tonic/stablemed", token=hf_token)
|
64 |
|
65 |
class ChatBot:
|
66 |
def __init__(self):
|
67 |
self.history = []
|
68 |
|
69 |
def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
# Update chat history
|
86 |
-
self.history = chat_history_ids
|
87 |
|
88 |
# Decode and return the response
|
89 |
-
response_text = tokenizer.decode(
|
90 |
return response_text
|
91 |
|
92 |
bot = ChatBot()
|
93 |
|
94 |
-
title = "ππ»Welcome to Tonic's
|
95 |
description = """
|
96 |
-
You can use this Space to test out the current model [
|
97 |
-
You can also use π·StableMedβοΈ on your laptop & by cloning this space. π§¬π¬π Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/
|
98 |
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)
|
99 |
"""
|
100 |
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."]]
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
|
6 |
hf_token = os.environ.get('HUGGINGFACE_TOKEN')
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# Define the device
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
+
# Load tokenizer and model
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained('stabilityai/stablelm-zephyr-3b', token=hf_token)
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
14 |
+
'stabilityai/stablelm-zephyr-3b',
|
15 |
+
trust_remote_code=True,
|
16 |
+
device_map="auto",
|
17 |
+
token=hf_token
|
18 |
+
)
|
19 |
+
model.to(device)
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
class ChatBot:
|
22 |
def __init__(self):
|
23 |
self.history = []
|
24 |
|
25 |
def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
|
26 |
+
prompt = [{'role': 'user', 'content': user_input}, {'role': 'system', 'content': system_prompt}]
|
27 |
+
inputs = tokenizer.apply_chat_template(
|
28 |
+
prompt,
|
29 |
+
add_generation_prompt=True,
|
30 |
+
return_tensors='pt'
|
31 |
+
)
|
32 |
+
|
33 |
+
# Generate a response using the model
|
34 |
+
tokens = model.generate(
|
35 |
+
inputs.to(model.device),
|
36 |
+
max_new_tokens=1024,
|
37 |
+
temperature=0.8,
|
38 |
+
do_sample=True
|
39 |
+
)
|
|
|
|
|
|
|
40 |
|
41 |
# Decode and return the response
|
42 |
+
response_text = tokenizer.decode(tokens[0], skip_special_tokens=False)
|
43 |
return response_text
|
44 |
|
45 |
bot = ChatBot()
|
46 |
|
47 |
+
title = "ππ»Welcome to πTonic'sπ½StableπLM 3BπChat"
|
48 |
description = """
|
49 |
+
You can use this Space to test out the current model [stabilityai/stablelm-zephyr-3b](https://huggingface.co/stabilityai/stablelm-zephyr-3b)
|
50 |
+
You can also use π·StableMedβοΈ on your laptop & by cloning this space. π§¬π¬π Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/TonicsStableLM3B?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>
|
51 |
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)
|
52 |
"""
|
53 |
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."]]
|