Update app.py
Browse files
app.py
CHANGED
@@ -5,41 +5,48 @@ import requests
|
|
5 |
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
6 |
MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-base")
|
7 |
|
8 |
-
headers = {
|
9 |
-
"Authorization": f"Bearer {HF_TOKEN}"
|
10 |
-
}
|
11 |
-
|
12 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
|
|
13 |
|
14 |
def query(payload):
|
15 |
-
response = requests.post(API_URL, headers=
|
16 |
-
if response.status_code
|
17 |
-
return response.
|
18 |
-
|
19 |
-
return {"error": "❌ Ошибка API: 404 Not Found"}
|
20 |
-
elif response.status_code == 401:
|
21 |
-
return {"error": "❌ Ошибка API: Unauthorized (проверь токен)"}
|
22 |
-
else:
|
23 |
-
return {"error": f"❌ Ошибка API: {response.status_code}"}
|
24 |
|
25 |
def chat(message, history):
|
|
|
|
|
|
|
26 |
history.append((message, ""))
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
else:
|
37 |
-
output = str(
|
|
|
|
|
|
|
|
|
38 |
|
|
|
39 |
history[-1] = (message, output)
|
40 |
return history, history
|
41 |
|
42 |
-
chat_ui = gr.ChatInterface(fn=chat, title="FlareGPT"
|
43 |
|
44 |
if __name__ == "__main__":
|
45 |
chat_ui.launch()
|
|
|
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 len(response["choices"]) > 0:
|
33 |
+
output = response["choices"][0].get("message", {}).get("content", "")
|
34 |
+
else:
|
35 |
+
output = str(response)
|
36 |
+
elif isinstance(response, list) and len(response) > 0 and "generated_text" in response[0]:
|
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()
|