DinoFrog commited on
Commit
73da356
·
verified ·
1 Parent(s): 6741a19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -28
app.py CHANGED
@@ -1,23 +1,18 @@
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 matplotlib.pyplot as plt
7
  import os
8
 
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
 
11
  # Fonction pour appeler l'API Zephyr
12
- def call_zephyr_api(prompt, hf_token=HF_TOKEN):
13
- API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
14
- headers = {"Authorization": f"Bearer {hf_token}"}
15
- payload = {"inputs": prompt, "parameters": {"max_new_tokens": 300}}
16
-
17
  try:
18
- response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
19
- response.raise_for_status()
20
- return response.json()[0]["generated_text"]
21
  except Exception as e:
22
  raise gr.Error(f"❌ Erreur d'appel API Hugging Face : {str(e)}")
23
 
@@ -54,24 +49,13 @@ def full_analysis(text, mode, detail_mode, count, history):
54
  result = classifier(text)[0]
55
  sentiment_output = f"Sentiment : {result['label']} (Score: {result['score']:.2f})"
56
 
57
- prompt = f"""<|system|>
58
- You are a professional financial analyst AI.
59
- </s>
60
- <|user|>
61
- Analyze the following financial news carefully:
62
- "{text}"
63
-
64
- The detected sentiment for this news is: {result['label'].lower()}.
65
 
66
- Now, explain why the sentiment is {result['label'].lower()} using a logical, fact-based explanation.
67
- Base your reasoning only on the given news text.
68
- Do not repeat the news text or the prompt.
69
- Respond only with your financial analysis in one clear paragraph.
70
- Write in a clear and professional tone.
71
- </s>
72
- <|assistant|>"""
73
-
74
- explanation_en = call_zephyr_api(prompt)
75
  explanation_fr = translator_to_fr(explanation_en, max_length=512)[0]['translation_text']
76
 
77
  count += 1
@@ -97,7 +81,6 @@ def download_history(history):
97
  # Interface Gradio
98
  def launch_app():
99
  with gr.Blocks(theme=gr.themes.Base(), css="body {background-color: #0D1117; color: white;} .gr-button {background-color: #161B22; border: 1px solid #30363D;}") as iface:
100
-
101
  gr.Markdown("# 📈 Analyse Financière Premium + Explication IA", elem_id="title")
102
  gr.Markdown("Entrez une actualité financière. L'IA analyse et explique en anglais/français. Choisissez votre mode d'explication.")
103
 
 
1
  import gradio as gr
 
2
  from transformers import pipeline
3
  from langdetect import detect
4
+ from huggingface_hub import InferenceClient
5
  import pandas as pd
 
6
  import os
7
 
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
 
10
  # Fonction pour appeler l'API Zephyr
11
+ def call_zephyr_api(messages, hf_token=HF_TOKEN):
12
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hf_token)
 
 
 
13
  try:
14
+ response = client.chat_completion(messages, max_tokens=300)
15
+ return response.choices[0].message.content
 
16
  except Exception as e:
17
  raise gr.Error(f"❌ Erreur d'appel API Hugging Face : {str(e)}")
18
 
 
49
  result = classifier(text)[0]
50
  sentiment_output = f"Sentiment : {result['label']} (Score: {result['score']:.2f})"
51
 
52
+ messages = [
53
+ {"role": "system", "content": "You are a professional financial analyst AI."},
54
+ {"role": "user", "content": f"Analyze the following financial news carefully:\n\"{text}\"\n\nThe detected sentiment for this news is: {result['label'].lower()}.\n\nNow, explain why the sentiment is {result['label'].lower()} using a logical, fact-based explanation.\nBase your reasoning only on the given news text.\nDo not repeat the news text or the prompt.\nRespond only with your financial analysis in one clear paragraph.\nWrite in a clear and professional tone."}
55
+ ]
56
+ explanation_en = call_zephyr_api(messages)
 
 
 
57
 
58
+ explanation_en = call_zephyr_api(messages)
 
 
 
 
 
 
 
 
59
  explanation_fr = translator_to_fr(explanation_en, max_length=512)[0]['translation_text']
60
 
61
  count += 1
 
81
  # Interface Gradio
82
  def launch_app():
83
  with gr.Blocks(theme=gr.themes.Base(), css="body {background-color: #0D1117; color: white;} .gr-button {background-color: #161B22; border: 1px solid #30363D;}") as iface:
 
84
  gr.Markdown("# 📈 Analyse Financière Premium + Explication IA", elem_id="title")
85
  gr.Markdown("Entrez une actualité financière. L'IA analyse et explique en anglais/français. Choisissez votre mode d'explication.")
86