Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
|
| 4 |
+
|
| 5 |
+
model_id = os.getenv("MODEL_ID", "mistralai/Mistral-7B-Instruct-v0.3")
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_id,
|
| 10 |
+
device_map="auto",
|
| 11 |
+
load_in_4bit=True,
|
| 12 |
+
trust_remote_code=True
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 16 |
+
|
| 17 |
+
def generate_response(message, history):
|
| 18 |
+
messages = history + [[message, ""]]
|
| 19 |
+
prompt = ""
|
| 20 |
+
for user, bot in messages:
|
| 21 |
+
prompt += f"<|user|>\n{user}\n<|assistant|>\n{bot}\n"
|
| 22 |
+
prompt += f"<|user|>\n{message}\n<|assistant|>\n"
|
| 23 |
+
|
| 24 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 25 |
+
output = model.generate(**inputs, max_new_tokens=512, streamer=streamer)
|
| 26 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True).split("<|assistant|>")[-1].strip()
|
| 27 |
+
|
| 28 |
+
return response
|
| 29 |
+
|
| 30 |
+
demo = gr.ChatInterface(fn=generate_response, title="Vynix AI", theme="soft")
|
| 31 |
+
|
| 32 |
+
if _name_ == "_main_":
|
| 33 |
+
demo.launch()
|