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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -8,11 +8,11 @@ import os
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,13 +49,23 @@ def full_analysis(text, mode, detail_mode, count, history):
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
 
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
 
10
  # Fonction pour appeler l'API Zephyr
11
+ def call_zephyr_api(prompt, hf_token=HF_TOKEN):
12
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hf_token)
13
  try:
14
+ response = client.text_generation(prompt, max_new_tokens=300)
15
+ return response
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
+ prompt = f"""<|system|>
53
+ You are a professional financial analyst AI.
54
+ </s>
55
+ <|user|>
56
+ Analyze the following financial news carefully:
57
+ "{text}"
58
+
59
+ The detected sentiment for this news is: {result['label'].lower()}.
60
+
61
+ Now, explain why the sentiment is {result['label'].lower()} using a logical, fact-based explanation.
62
+ Base your reasoning only on the given news text.
63
+ Do not repeat the news text or the prompt.
64
+ Respond only with your financial analysis in one clear paragraph.
65
+ Write in a clear and professional tone.
66
+ </s>
67
+ <|assistant|>"""
68
+ explanation_en = call_zephyr_api(prompt)
69
  explanation_fr = translator_to_fr(explanation_en, max_length=512)[0]['translation_text']
70
 
71
  count += 1