Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
MODEL_PATH = "SafaaAI/final_llm_darija_fr_tech"
|
| 6 |
+
|
| 7 |
+
# Charger le tokenizer et le modèle en float32 pour CPU
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
MODEL_PATH,
|
| 11 |
+
trust_remote_code=True,
|
| 12 |
+
torch_dtype=torch.float32
|
| 13 |
+
)
|
| 14 |
+
model.to("cpu")
|
| 15 |
+
model.eval()
|
| 16 |
+
|
| 17 |
+
def generate_response(prompt):
|
| 18 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model.generate(**inputs, max_new_tokens=200)
|
| 21 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 22 |
+
|
| 23 |
+
# Interface Gradio
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("## SafaaAI LLM Chat (Darija + FR + Technique)")
|
| 26 |
+
with gr.Row():
|
| 27 |
+
txt_input = gr.Textbox(label="Votre question", placeholder="Écrire ici...")
|
| 28 |
+
txt_output = gr.Textbox(label="Réponse du modèle")
|
| 29 |
+
btn = gr.Button("Envoyer")
|
| 30 |
+
btn.click(fn=generate_response, inputs=txt_input, outputs=txt_output)
|
| 31 |
+
|
| 32 |
+
demo.launch()
|