DinoFrog commited on
Commit
e29f507
·
verified ·
1 Parent(s): f8fa994

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -13
app.py CHANGED
@@ -1,22 +1,31 @@
1
  import gradio as gr
 
2
  from transformers import pipeline
3
  from langdetect import detect
4
  import pandas as pd
5
  import textstat
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Chargement du modèle de sentiment
8
- classifier = pipeline(
9
- "sentiment-analysis",
10
- model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
11
- )
12
 
13
  # Modèles de traduction
14
  translator_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
15
  translator_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
16
 
17
- # Modèle explicatif CPU-friendly
18
- explainer = pipeline("text2text-generation", model="facebook/blenderbot-1B-distill")
19
-
20
  # Fonction pour suggérer le meilleur modèle
21
  def suggest_model(text):
22
  word_count = len(text.split())
@@ -27,10 +36,25 @@ def suggest_model(text):
27
  else:
28
  return "Précis"
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  # Fonction d'analyse
31
- def full_analysis(text, mode, detail_mode, count, history):
32
  if not text:
33
- return "Entrez une phrase.", "", "", 0, history, None
34
 
35
  try:
36
  lang = detect(text)
@@ -50,10 +74,11 @@ def full_analysis(text, mode, detail_mode, count, history):
50
  {"Write a concise paragraph." if detail_mode == "Normal" else "Write a detailed explanation over multiple paragraphs."}
51
  """
52
 
53
- explanation_en = explainer(prompt, max_length=300 if detail_mode == "Expert" else 150)[0]['generated_text']
54
  explanation_fr = translator_to_fr(explanation_en, max_length=512)[0]['translation_text']
55
 
56
  clarity_score = textstat.flesch_reading_ease(explanation_en)
 
57
 
58
  count += 1
59
  history.append({
@@ -65,7 +90,7 @@ def full_analysis(text, mode, detail_mode, count, history):
65
  "Clarté": f"{clarity_score:.1f}"
66
  })
67
 
68
- return sentiment_output, explanation_en, explanation_fr, clarity_score, count, history, None
69
 
70
  # Fonction pour télécharger historique CSV
71
  def download_history(history):
@@ -85,6 +110,7 @@ def launch_app():
85
 
86
  count = gr.State(0)
87
  history = gr.State([])
 
88
 
89
  with gr.Row():
90
  input_text = gr.Textbox(lines=4, placeholder="Entrez une actualité ici...", label="Texte à analyser")
@@ -102,6 +128,7 @@ def launch_app():
102
  )
103
 
104
  analyze_btn = gr.Button("Analyser")
 
105
  download_btn = gr.Button("Télécharger CSV")
106
 
107
  with gr.Row():
@@ -114,14 +141,20 @@ def launch_app():
114
  explanation_output_fr = gr.Textbox(label="Explication en Français")
115
 
116
  clarity_score_output = gr.Textbox(label="Score de Clarté (Flesch Reading Ease)")
 
117
  download_file = gr.File(label="Fichier CSV")
118
 
119
  input_text.change(lambda t: gr.update(value=suggest_model(t)), inputs=[input_text], outputs=[mode_selector])
120
 
121
  analyze_btn.click(
122
  full_analysis,
123
- inputs=[input_text, mode_selector, detail_mode_selector, count, history],
124
- outputs=[sentiment_output, explanation_output_en, explanation_output_fr, clarity_score_output, count, history, download_file]
 
 
 
 
 
125
  )
126
 
127
  download_btn.click(
 
1
  import gradio as gr
2
+ import requests
3
  from transformers import pipeline
4
  from langdetect import detect
5
  import pandas as pd
6
  import textstat
7
+ import matplotlib.pyplot as plt
8
+ import os
9
+
10
+ HF_TOKEN = os.getenv("HF_TOKEN")
11
+
12
+
13
+ # Fonction pour appeler l'API Mistral-7B
14
+ def call_mistral_api(prompt, hf_token=HF_TOKEN):
15
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
16
+ headers = {"Authorization": f"Bearer {hf_token}"}
17
+ payload = {"inputs": prompt, "parameters": {"max_new_tokens": 300}}
18
+ response = requests.post(API_URL, headers=headers, json=payload)
19
+ response.raise_for_status()
20
+ return response.json()[0]["generated_text"]
21
 
22
  # Chargement du modèle de sentiment
23
+ classifier = pipeline("sentiment-analysis", model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
 
 
 
24
 
25
  # Modèles de traduction
26
  translator_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
27
  translator_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
28
 
 
 
 
29
  # Fonction pour suggérer le meilleur modèle
30
  def suggest_model(text):
31
  word_count = len(text.split())
 
36
  else:
37
  return "Précis"
38
 
39
+ # Fonction pour générer un graphique de clarté
40
+ def plot_clarity(clarity_scores):
41
+ plt.figure(figsize=(8, 4))
42
+ plt.plot(range(1, len(clarity_scores) + 1), clarity_scores, marker='o')
43
+ plt.title("Évolution du Score de Clarté")
44
+ plt.xlabel("Numéro d'analyse")
45
+ plt.ylabel("Score de Clarté")
46
+ plt.ylim(0, 100)
47
+ plt.grid(True)
48
+ return plt.gcf()
49
+
50
+ # Fonction pour reset le graphique
51
+ def reset_clarity_graph():
52
+ return [], plot_clarity([])
53
+
54
  # Fonction d'analyse
55
+ def full_analysis(text, mode, detail_mode, count, history, clarity_scores):
56
  if not text:
57
+ return "Entrez une phrase.", "", "", 0, history, clarity_scores, None, None
58
 
59
  try:
60
  lang = detect(text)
 
74
  {"Write a concise paragraph." if detail_mode == "Normal" else "Write a detailed explanation over multiple paragraphs."}
75
  """
76
 
77
+ explanation_en = call_mistral_api(prompt)
78
  explanation_fr = translator_to_fr(explanation_en, max_length=512)[0]['translation_text']
79
 
80
  clarity_score = textstat.flesch_reading_ease(explanation_en)
81
+ clarity_scores.append(clarity_score)
82
 
83
  count += 1
84
  history.append({
 
90
  "Clarté": f"{clarity_score:.1f}"
91
  })
92
 
93
+ return sentiment_output, explanation_en, explanation_fr, clarity_score, count, history, clarity_scores, plot_clarity(clarity_scores)
94
 
95
  # Fonction pour télécharger historique CSV
96
  def download_history(history):
 
110
 
111
  count = gr.State(0)
112
  history = gr.State([])
113
+ clarity_scores = gr.State([])
114
 
115
  with gr.Row():
116
  input_text = gr.Textbox(lines=4, placeholder="Entrez une actualité ici...", label="Texte à analyser")
 
128
  )
129
 
130
  analyze_btn = gr.Button("Analyser")
131
+ reset_graph_btn = gr.Button("Reset Graphique")
132
  download_btn = gr.Button("Télécharger CSV")
133
 
134
  with gr.Row():
 
141
  explanation_output_fr = gr.Textbox(label="Explication en Français")
142
 
143
  clarity_score_output = gr.Textbox(label="Score de Clarté (Flesch Reading Ease)")
144
+ clarity_plot = gr.Plot(label="Graphique des Scores de Clarté")
145
  download_file = gr.File(label="Fichier CSV")
146
 
147
  input_text.change(lambda t: gr.update(value=suggest_model(t)), inputs=[input_text], outputs=[mode_selector])
148
 
149
  analyze_btn.click(
150
  full_analysis,
151
+ inputs=[input_text, mode_selector, detail_mode_selector, count, history, clarity_scores],
152
+ outputs=[sentiment_output, explanation_output_en, explanation_output_fr, clarity_score_output, count, history, clarity_scores, clarity_plot]
153
+ )
154
+
155
+ reset_graph_btn.click(
156
+ reset_clarity_graph,
157
+ outputs=[clarity_scores, clarity_plot]
158
  )
159
 
160
  download_btn.click(