Update app.py
Browse files
app.py
CHANGED
@@ -2,51 +2,70 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
|
|
|
5 |
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
6 |
MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-base")
|
7 |
|
|
|
8 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
9 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
10 |
|
|
|
|
|
11 |
def query(payload):
|
|
|
12 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
|
|
|
|
|
|
13 |
if response.status_code != 200:
|
14 |
return f"❌ Ошибка API: {response.status_code} {response.text}"
|
15 |
return response.json()
|
16 |
|
|
|
|
|
17 |
def chat(message, history):
|
18 |
if history is None:
|
19 |
history = []
|
20 |
-
|
|
|
21 |
history.append((message, ""))
|
22 |
|
|
|
23 |
response = query({"inputs": message})
|
24 |
|
25 |
-
#
|
26 |
output = ""
|
27 |
if isinstance(response, dict):
|
28 |
if "error" in response:
|
29 |
-
output = response["error"]
|
30 |
elif "generated_text" in response:
|
31 |
output = response["generated_text"]
|
32 |
-
elif isinstance(response.get("choices"), list) and
|
33 |
output = response["choices"][0].get("message", {}).get("content", "")
|
34 |
else:
|
35 |
output = str(response)
|
36 |
-
elif isinstance(response, list) and
|
37 |
output = response[0]["generated_text"]
|
38 |
else:
|
39 |
output = str(response)
|
40 |
|
41 |
-
# Гарантируем, что output — строка
|
42 |
if not isinstance(output, str):
|
43 |
output = str(output)
|
44 |
|
45 |
-
# Обновляем последний элемент в истории — теперь с ответом
|
46 |
history[-1] = (message, output)
|
47 |
return history, history
|
48 |
|
49 |
-
chat_ui = gr.ChatInterface(fn=chat, title="FlareGPT")
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
if __name__ == "__main__":
|
52 |
chat_ui.launch()
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
|
5 |
+
# Загружаем токен и имя модели из переменных окружения
|
6 |
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
7 |
MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-base")
|
8 |
|
9 |
+
# URL запроса к модели через Hugging Face Inference API
|
10 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
11 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
12 |
|
13 |
+
|
14 |
+
# Функция запроса к модели
|
15 |
def query(payload):
|
16 |
+
print("⏳ Отправка запроса к модели...")
|
17 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
18 |
+
print("📡 Статус:", response.status_code)
|
19 |
+
print("📩 Ответ:", response.text)
|
20 |
+
|
21 |
if response.status_code != 200:
|
22 |
return f"❌ Ошибка API: {response.status_code} {response.text}"
|
23 |
return response.json()
|
24 |
|
25 |
+
|
26 |
+
# Функция чата
|
27 |
def chat(message, history):
|
28 |
if history is None:
|
29 |
history = []
|
30 |
+
|
31 |
+
print("👤 Сообщение пользователя:", message)
|
32 |
history.append((message, ""))
|
33 |
|
34 |
+
# Запрос к модели
|
35 |
response = query({"inputs": message})
|
36 |
|
37 |
+
# Обработка ответа
|
38 |
output = ""
|
39 |
if isinstance(response, dict):
|
40 |
if "error" in response:
|
41 |
+
output = "❌ Ошибка: " + response["error"]
|
42 |
elif "generated_text" in response:
|
43 |
output = response["generated_text"]
|
44 |
+
elif isinstance(response.get("choices"), list) and response["choices"]:
|
45 |
output = response["choices"][0].get("message", {}).get("content", "")
|
46 |
else:
|
47 |
output = str(response)
|
48 |
+
elif isinstance(response, list) and "generated_text" in response[0]:
|
49 |
output = response[0]["generated_text"]
|
50 |
else:
|
51 |
output = str(response)
|
52 |
|
|
|
53 |
if not isinstance(output, str):
|
54 |
output = str(output)
|
55 |
|
|
|
56 |
history[-1] = (message, output)
|
57 |
return history, history
|
58 |
|
|
|
59 |
|
60 |
+
# Интерфейс Gradio
|
61 |
+
chat_ui = gr.ChatInterface(
|
62 |
+
fn=chat,
|
63 |
+
title="FlareGPT",
|
64 |
+
retry_btn="🔄 Retry",
|
65 |
+
undo_btn="↩️ Undo",
|
66 |
+
clear_btn="🗑️ Clear"
|
67 |
+
)
|
68 |
+
|
69 |
+
# Запуск
|
70 |
if __name__ == "__main__":
|
71 |
chat_ui.launch()
|