Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,50 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return f"{label} ({confidence.item():.2%} confidence)"
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
demo = gr.Interface(fn=detect_language, inputs="text", outputs="text", title="Language Detection")
|
| 26 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
# Path to local model in the same repo (e.g., "Mixtral-8x7B-Instruct-v0.1" folder uploaded to Space)
|
| 6 |
+
MODEL_DIR = "Mixtral-8x7B-Instruct-v0.1"
|
| 7 |
+
|
| 8 |
+
# Load tokenizer and model
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
MODEL_DIR,
|
| 12 |
+
torch_dtype=torch.float16,
|
| 13 |
+
device_map="auto",
|
| 14 |
+
trust_remote_code=True
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Generation function
|
| 18 |
+
def generate_text(prompt):
|
| 19 |
+
messages = [{"role": "user", "content": prompt}]
|
| 20 |
+
inputs = tokenizer.apply_chat_template(
|
| 21 |
+
messages,
|
| 22 |
+
return_tensors="pt",
|
| 23 |
+
add_generation_prompt=True
|
| 24 |
+
).to(model.device)
|
| 25 |
|
| 26 |
+
output = model.generate(
|
| 27 |
+
**inputs,
|
| 28 |
+
max_new_tokens=300,
|
| 29 |
+
temperature=0.7,
|
| 30 |
+
top_p=0.95,
|
| 31 |
+
do_sample=True,
|
| 32 |
+
pad_token_id=tokenizer.eos_token_id
|
| 33 |
+
)
|
| 34 |
|
| 35 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 36 |
+
if prompt in decoded:
|
| 37 |
+
return decoded.split(prompt)[-1].strip()
|
| 38 |
+
return decoded.strip()
|
| 39 |
|
| 40 |
+
# Gradio interface
|
| 41 |
+
demo = gr.Interface(
|
| 42 |
+
fn=generate_text,
|
| 43 |
+
inputs=gr.Textbox(lines=4, label="Enter your message (FR / AR / EN...)"),
|
| 44 |
+
outputs="text",
|
| 45 |
+
title="🧠 Mixtral 8x7B Instruct Chat",
|
| 46 |
+
description="Multilingual response generation with Mistral Mixtral 8x7B Instruct model.",
|
| 47 |
+
)
|
|
|
|
| 48 |
|
| 49 |
+
# Launch
|
|
|
|
| 50 |
demo.launch()
|