madibaalbert commited on
Commit
b379801
·
verified ·
1 Parent(s): 81852f8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import os
4
+
5
+ # --- CONFIGURATION DU MODÈLE ---
6
+ # Utilisation d'un modèle léger pour garantir la fluidité sur le CPU gratuit de Hugging Face
7
+ MODEL_ID = "distilbert-base-uncased-finetuned-sst-2-english"
8
+
9
+ print(f"Loading model: {MODEL_ID}...")
10
+ # On initialise le pipeline d'analyse de sentiment
11
+ # Note: Le téléchargement se fait automatiquement au premier lancement dans le Space
12
+ classifier = pipeline("sentiment-analysis", model=MODEL_ID)
13
+
14
+ def predict_sentiment(text):
15
+ """
16
+ Fonction de traitement pour l'inférence.
17
+ Elle sera exposée via l'endpoint API.
18
+ """
19
+ if not text or text.strip() == "":
20
+ return "Veuillez entrer un texte valide."
21
+
22
+ try:
23
+ results = classifier(text)
24
+ label = results[0]['label']
25
+ score = round(results[0]['score'], 4)
26
+ return f"Sentiment: {label} (Confiance: {score})"
27
+ except Exception as e:
28
+ return f"Erreur lors de l'analyse: {str(e)}"
29
+
30
+ # --- CONSTRUCTION DE L'INTERFACE GRADIO ---
31
+ # Utilisation de gr.Blocks pour un design plus "Pro" (OmniGroup Style)
32
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
33
+ gr.Markdown(
34
+ """
35
+ # 🌐 OmniGroup AI - Sentiment Analysis API
36
+ ### Déploiement souverain pour l'écosystème Pangea.
37
+ Ce Space expose un endpoint gratuit pour analyser les sentiments textuels.
38
+ """
39
+ )
40
+
41
+ with gr.Row():
42
+ with gr.Column():
43
+ input_text = gr.Textbox(
44
+ label="Texte à analyser",
45
+ placeholder="Entrez votre phrase ici...",
46
+ lines=3
47
+ )
48
+ submit_btn = gr.Button("Analyser", variant="primary")
49
+
50
+ with gr.Column():
51
+ output_text = gr.Textbox(label="Résultat de l'API")
52
+
53
+ # Liaison du bouton et de la touche 'Entrée' à la fonction de prédiction
54
+ submit_btn.click(fn=predict_sentiment, inputs=input_text, outputs=output_text, api_name="predict")
55
+ input_text.submit(fn=predict_sentiment, inputs=input_text, outputs=output_text)
56
+
57
+ gr.Markdown(
58
+ """
59
+ ---
60
+ **Note technique :** Pour utiliser cet endpoint via Python, utilisez le `gradio_client` :
61
+ ```python
62
+ from gradio_client import Client
63
+ client = Client("votre-username/nom-du-space")
64
+ result = client.predict("Texte", api_name="/predict")
65
+ ```
66
+ """
67
+ )
68
+
69
+ # --- LANCEMENT ---
70
+ if __name__ == "__main__":
71
+ # Hugging Face Spaces nécessite que le serveur écoute sur 0.0.0.0:7860 (par défaut via launch)
72
+ demo.launch(server_name="0.0.0.0", server_port=7860)