HaveAI commited on
Commit
904faef
·
verified ·
1 Parent(s): d7464d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -1,12 +1,10 @@
1
  import gradio as gr
2
  import os
3
  import requests
4
- from dotenv import load_dotenv
5
-
6
- load_dotenv()
7
 
 
8
  HF_TOKEN = os.getenv("HF_API_TOKEN")
9
- MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-base")
10
 
11
  API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
12
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
@@ -15,10 +13,19 @@ def chat_fn(message, history):
15
  try:
16
  prompt = f"Answer the question: {message}"
17
  payload = {"inputs": prompt}
 
18
  response = requests.post(API_URL, headers=HEADERS, json=payload)
19
- result = response.json()
20
- print(result)
21
 
 
 
 
 
 
 
 
 
 
 
22
  if isinstance(result, list) and "generated_text" in result[0]:
23
  return result[0]["generated_text"]
24
  elif isinstance(result, dict) and "generated_text" in result:
@@ -26,12 +33,12 @@ def chat_fn(message, history):
26
  else:
27
  return "❌ Ответ не распознан"
28
  except Exception as e:
29
- return f"❌ Ошибка: {str(e)}"
30
 
31
  gr.ChatInterface(
32
  fn=chat_fn,
33
  title="FlareGPT",
34
- retry_btn="🔄 Retry",
35
- undo_btn="↩️ Undo",
36
- clear_btn="🗑️ Clear",
37
  ).launch()
 
1
  import gradio as gr
2
  import os
3
  import requests
 
 
 
4
 
5
+ # Получаем переменные окружения (лучше задать их в интерфейсе Hugging Face → Variables)
6
  HF_TOKEN = os.getenv("HF_API_TOKEN")
7
+ MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-small")
8
 
9
  API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
10
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
 
13
  try:
14
  prompt = f"Answer the question: {message}"
15
  payload = {"inputs": prompt}
16
+
17
  response = requests.post(API_URL, headers=HEADERS, json=payload)
 
 
18
 
19
+ # Проверка кода ответа
20
+ if response.status_code != 200:
21
+ return f"❌ Ошибка API: {response.status_code}\n{response.text}"
22
+
23
+ try:
24
+ result = response.json()
25
+ except Exception:
26
+ return f"❌ Неверный ответ от API (не JSON):\n{response.text}"
27
+
28
+ # Извлечение текста из ответа
29
  if isinstance(result, list) and "generated_text" in result[0]:
30
  return result[0]["generated_text"]
31
  elif isinstance(result, dict) and "generated_text" in result:
 
33
  else:
34
  return "❌ Ответ не распознан"
35
  except Exception as e:
36
+ return f"❌ Общая ошибка: {str(e)}"
37
 
38
  gr.ChatInterface(
39
  fn=chat_fn,
40
  title="FlareGPT",
41
+ retry_btn="🔄 Повторить",
42
+ undo_btn="↩️ Назад",
43
+ clear_btn="🗑️ Очистить",
44
  ).launch()