Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,8 @@ 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,27 +12,22 @@ def chat_fn(message, history):
|
|
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"❌ Неверный
|
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:
|
32 |
-
return result["generated_text"]
|
33 |
else:
|
34 |
return "❌ Ответ не распознан"
|
35 |
except Exception as e:
|
36 |
-
return f"❌
|
37 |
|
38 |
gr.ChatInterface(
|
39 |
fn=chat_fn,
|
|
|
2 |
import os
|
3 |
import requests
|
4 |
|
|
|
5 |
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
6 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-small") # Обязательно существующая модель
|
7 |
|
8 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
9 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
|
12 |
try:
|
13 |
prompt = f"Answer the question: {message}"
|
14 |
payload = {"inputs": prompt}
|
|
|
15 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
16 |
|
|
|
17 |
if response.status_code != 200:
|
18 |
return f"❌ Ошибка API: {response.status_code}\n{response.text}"
|
19 |
|
20 |
try:
|
21 |
result = response.json()
|
22 |
except Exception:
|
23 |
+
return f"❌ Неверный JSON:\n{response.text}"
|
24 |
|
|
|
25 |
if isinstance(result, list) and "generated_text" in result[0]:
|
26 |
return result[0]["generated_text"]
|
|
|
|
|
27 |
else:
|
28 |
return "❌ Ответ не распознан"
|
29 |
except Exception as e:
|
30 |
+
return f"❌ Ошибка: {str(e)}"
|
31 |
|
32 |
gr.ChatInterface(
|
33 |
fn=chat_fn,
|