HaveAI commited on
Commit
9e594ee
·
verified ·
1 Parent(s): 43f402b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -18
app.py CHANGED
@@ -1,45 +1,63 @@
 
1
  import gradio as gr
2
  import requests
3
- import os
4
 
5
- # Получаем переменные окружения
6
  API_TOKEN = os.getenv("HF_API_TOKEN")
7
  MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-small")
8
  API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
9
-
10
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
11
 
 
12
  def query_huggingface(prompt):
 
 
 
13
  payload = {"inputs": prompt}
14
- response = requests.post(API_URL, headers=headers, json=payload)
 
 
 
 
 
 
 
15
 
16
  try:
17
  result = response.json()
18
- if isinstance(result, list) and "generated_text" in result[0]:
19
- return result[0]["generated_text"]
20
- elif isinstance(result, dict) and "generated_text" in result:
21
- return result["generated_text"]
22
- else:
23
- return f"⚠️ Ответ не распознан: {result}"
24
  except Exception as e:
25
- return f"❌ Ошибка: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  def chat(user_input, history):
28
  history = history or []
29
  prompt = f"Q: {user_input}\nA:"
30
- output = query_huggingface(prompt)
31
- history.append((user_input, output))
32
  return history, history
33
 
 
 
34
  with gr.Blocks() as demo:
35
- gr.Markdown("## 🤖 Чат с моделью Hugging Face")
36
- chatbot = gr.Chatbot()
37
- msg = gr.Textbox(placeholder="Введите сообщение и нажмите Enter...")
38
- clear = gr.Button("Очистить")
 
39
 
40
  state = gr.State([])
41
 
42
- msg.submit(chat, [msg, state], [chatbot, state])
43
  clear.click(lambda: ([], []), None, [chatbot, state])
44
 
45
  demo.launch()
 
1
+ import os
2
  import gradio as gr
3
  import requests
 
4
 
5
+ # Загружаем токен и имя модели из secrets
6
  API_TOKEN = os.getenv("HF_API_TOKEN")
7
  MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-small")
8
  API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
 
9
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
10
 
11
+
12
  def query_huggingface(prompt):
13
+ if not API_TOKEN:
14
+ return "❌ Токен HF_API_TOKEN не задан! Добавь его в Secrets."
15
+
16
  payload = {"inputs": prompt}
17
+
18
+ try:
19
+ response = requests.post(API_URL, headers=headers, json=payload)
20
+ except Exception as e:
21
+ return f"🚫 Ошибка подключения: {str(e)}"
22
+
23
+ if response.status_code != 200:
24
+ return f"❌ Ошибка API: {response.status_code}\n{response.text}"
25
 
26
  try:
27
  result = response.json()
 
 
 
 
 
 
28
  except Exception as e:
29
+ return f"❌ Ошибка парсинга ответа: {str(e)}\nОтвет: {response.text}"
30
+
31
+ # Разбор разных форматов
32
+ if isinstance(result, list) and "generated_text" in result[0]:
33
+ return result[0]["generated_text"]
34
+ elif isinstance(result, dict) and "generated_text" in result:
35
+ return result["generated_text"]
36
+ elif isinstance(result, dict) and "error" in result:
37
+ return f"⚠️ Ошибка модели: {result['error']}"
38
+ else:
39
+ return f"⚠️ Неожиданный формат ответа: {result}"
40
+
41
 
42
  def chat(user_input, history):
43
  history = history or []
44
  prompt = f"Q: {user_input}\nA:"
45
+ response = query_huggingface(prompt)
46
+ history.append((user_input, response))
47
  return history, history
48
 
49
+
50
+ # UI через Gradio Blocks
51
  with gr.Blocks() as demo:
52
+ gr.Markdown("## 🤖 FlareGPT — чат с моделью Hugging Face")
53
+
54
+ chatbot = gr.Chatbot(height=400)
55
+ message = gr.Textbox(placeholder="Введите сообщение и нажмите Enter...")
56
+ clear = gr.Button("🧹 Очистить")
57
 
58
  state = gr.State([])
59
 
60
+ message.submit(chat, [message, state], [chatbot, state])
61
  clear.click(lambda: ([], []), None, [chatbot, state])
62
 
63
  demo.launch()