Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,42 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
|
| 4 |
|
| 5 |
-
model_id =
|
| 6 |
|
| 7 |
-
tokenizer
|
|
|
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
model_id,
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
trust_remote_code=True
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
+
model_id = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 7 |
|
| 8 |
+
# Load tokenizer and model with correct settings
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False) # Important fix
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
model_id,
|
| 12 |
+
torch_dtype=torch.float16,
|
| 13 |
+
device_map="auto"
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Create generation pipeline
|
| 17 |
+
pipe = pipeline(
|
| 18 |
+
"text-generation",
|
| 19 |
+
model=model,
|
| 20 |
+
tokenizer=tokenizer,
|
| 21 |
+
max_new_tokens=512,
|
| 22 |
+
do_sample=True,
|
| 23 |
+
top_k=50,
|
| 24 |
+
top_p=0.95,
|
| 25 |
+
temperature=0.7,
|
| 26 |
+
repetition_penalty=1.1
|
| 27 |
+
)
|
| 28 |
|
| 29 |
+
# Define Gradio UI
|
| 30 |
+
def chat_fn(message, history):
|
| 31 |
+
prompt = f"[INST] {message.strip()} [/INST]"
|
| 32 |
+
output = pipe(prompt)[0]['generated_text']
|
| 33 |
+
return output.replace(prompt, "").strip()
|
| 34 |
|
| 35 |
+
chatbot = gr.ChatInterface(
|
| 36 |
+
fn=chat_fn,
|
| 37 |
+
title="π€ Vynix AI - Powered by Mistral",
|
| 38 |
+
description="Ask anything! Built using Mistral-7B-Instruct-v0.3.",
|
| 39 |
+
)
|
| 40 |
|
| 41 |
+
# Launch the app
|
| 42 |
+
chatbot.launch()
|