pheodoraa commited on
Commit
3a4b85a
·
verified ·
1 Parent(s): 04183ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -20
app.py CHANGED
@@ -1,26 +1,50 @@
1
- import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
- import torch.nn.functional as F
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- # Modèle de détection de langue
7
- model_name = "papluca/xlm-roberta-base-language-detection"
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
 
 
 
 
10
 
11
- # Récupération des étiquettes de langue depuis la config
12
- id2label = model.config.id2label
 
 
13
 
14
- # Fonction de détection de langue
15
- def detect_language(text):
16
- inputs = tokenizer(text, return_tensors="pt", truncation=True)
17
- with torch.no_grad():
18
- outputs = model(**inputs)
19
- probs = F.softmax(outputs.logits, dim=1)
20
- confidence, predicted_class = torch.max(probs, dim=1)
21
- label = id2label[predicted_class.item()]
22
- return f"{label} ({confidence.item():.2%} confidence)"
23
 
24
- # Interface Gradio
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()