Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,66 @@
|
|
1 |
-
from transformers import
|
2 |
-
from peft import PeftModel, PeftConfig
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Use the base model's ID
|
8 |
base_model_id = "mistralai/Mistral-7B-v0.1"
|
9 |
model_directory = "Tonic/mistralmed"
|
10 |
|
11 |
-
# Instantiate the
|
12 |
-
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True, padding_side="left")
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
|
17 |
# Specify the configuration class for the model
|
@@ -29,9 +78,9 @@ class ChatBot:
|
|
29 |
def __init__(self):
|
30 |
self.history = []
|
31 |
|
32 |
-
def predict(self,
|
33 |
# Encode user input
|
34 |
-
user_input_ids = tokenizer.encode(
|
35 |
|
36 |
# Concatenate the user input with chat history
|
37 |
if len(self.history) > 0:
|
@@ -53,7 +102,7 @@ bot = ChatBot()
|
|
53 |
|
54 |
title = "👋🏻Welcome to Tonic's MistralMed Chat🚀"
|
55 |
description = "You can use this Space to test out the current model (MistralMed) or duplicate this Space and use it for any other model on 🤗HuggingFace. Join me on Discord to build together."
|
56 |
-
examples = [["What is the boiling point of nitrogen"]]
|
57 |
|
58 |
iface = gr.Interface(
|
59 |
fn=bot.predict,
|
|
|
1 |
+
from transformers import AutoTokenizer, MistralForCausalLM
|
|
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
+
import random
|
5 |
+
from textwrap import wrap
|
6 |
+
import random
|
7 |
+
|
8 |
+
# Functions to Wrap the Prompt Correctly
|
9 |
+
|
10 |
+
def wrap_text(text, width=90):
|
11 |
+
lines = text.split('\n')
|
12 |
+
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
13 |
+
wrapped_text = '\n'.join(wrapped_lines)
|
14 |
+
return wrapped_text
|
15 |
+
|
16 |
+
def multimodal_prompt(input_text, system_prompt="", max_length=512):
|
17 |
+
"""
|
18 |
+
Generates text using a large language model, given a prompt and a device.
|
19 |
+
Args:
|
20 |
+
input_text: The input text to generate a response for.
|
21 |
+
system_prompt: Optional system prompt.
|
22 |
+
max_length: Maximum length of the generated text.
|
23 |
+
Returns:
|
24 |
+
A string containing the generated text.
|
25 |
+
"""
|
26 |
+
# Modify the input text to include the desired format
|
27 |
+
formatted_input = f"""<s>[INST]{input_text}[/INST]"""
|
28 |
+
|
29 |
+
# Encode the input text
|
30 |
+
encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
|
31 |
+
model_inputs = encodeds.to(device)
|
32 |
+
|
33 |
+
# Generate a response using the model
|
34 |
+
output = model.generate(
|
35 |
+
**model_inputs,
|
36 |
+
max_length=max_length,
|
37 |
+
use_cache=True,
|
38 |
+
early_stopping=True,
|
39 |
+
bos_token_id=model.config.bos_token_id,
|
40 |
+
eos_token_id=model.config.eos_token_id,
|
41 |
+
pad_token_id=model.config.eos_token_id,
|
42 |
+
temperature=0.1,
|
43 |
+
do_sample=True
|
44 |
+
)
|
45 |
+
|
46 |
+
# Decode the response
|
47 |
+
response_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
48 |
+
|
49 |
+
return response_text
|
50 |
+
|
51 |
+
|
52 |
+
# Define the device
|
53 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
54 |
+
|
55 |
# Use the base model's ID
|
56 |
base_model_id = "mistralai/Mistral-7B-v0.1"
|
57 |
model_directory = "Tonic/mistralmed"
|
58 |
|
59 |
+
# Instantiate the Tokenizer
|
60 |
+
# tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True, padding_side="left")
|
61 |
+
tokenizer = AutoTokenizer.from_pretrained("Tonic/mistralmed", trust_remote_code=True, padding_side="left")
|
62 |
+
tokenizer.pad_token = tokenizer.eos_token
|
63 |
+
tokenizer.padding_side = 'left'
|
64 |
|
65 |
|
66 |
# Specify the configuration class for the model
|
|
|
78 |
def __init__(self):
|
79 |
self.history = []
|
80 |
|
81 |
+
def predict(self, input_text):
|
82 |
# Encode user input
|
83 |
+
user_input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
84 |
|
85 |
# Concatenate the user input with chat history
|
86 |
if len(self.history) > 0:
|
|
|
102 |
|
103 |
title = "👋🏻Welcome to Tonic's MistralMed Chat🚀"
|
104 |
description = "You can use this Space to test out the current model (MistralMed) or duplicate this Space and use it for any other model on 🤗HuggingFace. Join me on Discord to build together."
|
105 |
+
examples = [["What is the boiling point of nitrogen?"]]
|
106 |
|
107 |
iface = gr.Interface(
|
108 |
fn=bot.predict,
|