File size: 1,430 Bytes
9dc0d26
3a4b85a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f66b24b
3a4b85a
 
 
 
 
 
 
 
f66b24b
3a4b85a
 
 
 
9dc0d26
3a4b85a
 
 
 
 
 
 
 
9dc0d26
3a4b85a
f66b24b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import torch
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM

# Path to local model in the same repo (e.g., "Mixtral-8x7B-Instruct-v0.1" folder uploaded to Space)
MODEL_DIR = "Mixtral-8x7B-Instruct-v0.1"

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_DIR,
    torch_dtype=torch.float16,
    device_map="auto",
    trust_remote_code=True
)

# Generation function
def generate_text(prompt):
    messages = [{"role": "user", "content": prompt}]
    inputs = tokenizer.apply_chat_template(
        messages,
        return_tensors="pt",
        add_generation_prompt=True
    ).to(model.device)

    output = model.generate(
        **inputs,
        max_new_tokens=300,
        temperature=0.7,
        top_p=0.95,
        do_sample=True,
        pad_token_id=tokenizer.eos_token_id
    )

    decoded = tokenizer.decode(output[0], skip_special_tokens=True)
    if prompt in decoded:
        return decoded.split(prompt)[-1].strip()
    return decoded.strip()

# Gradio interface
demo = gr.Interface(
    fn=generate_text,
    inputs=gr.Textbox(lines=4, label="Enter your message (FR / AR / EN...)"),
    outputs="text",
    title="🧠 Mixtral 8x7B Instruct Chat",
    description="Multilingual response generation with Mistral Mixtral 8x7B Instruct model.",
)

# Launch
demo.launch()