File size: 1,250 Bytes
b986c5c
2655d77
b986c5c
 
8ffe745
0072304
9e594ee
8ffe745
a788b34
9e594ee
2655d77
 
59fd87d
 
2655d77
 
904faef
 
 
 
 
 
0072304
904faef
2655d77
 
a788b34
2655d77
 
0072304
b986c5c
2655d77
 
cb7d534
904faef
 
 
2655d77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import gradio as gr
import os
import requests

HF_TOKEN = os.getenv("HF_API_TOKEN")
MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-small")  # Обязательно существующая модель

API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}

def chat_fn(message, history):
    try:
        prompt = f"Answer the question: {message}"
        payload = {"inputs": prompt}
        response = requests.post(API_URL, headers=HEADERS, json=payload)

        if response.status_code != 200:
            return f"❌ Ошибка API: {response.status_code}\n{response.text}"

        try:
            result = response.json()
        except Exception:
            return f"❌ Неверный JSON:\n{response.text}"

        if isinstance(result, list) and "generated_text" in result[0]:
            return result[0]["generated_text"]
        else:
            return "❌ Ответ не распознан"
    except Exception as e:
        return f"❌ Ошибка: {str(e)}"

gr.ChatInterface(
    fn=chat_fn,
    title="FlareGPT",
    retry_btn="🔄 Повторить",
    undo_btn="↩️ Назад",
    clear_btn="🗑️ Очистить",
).launch()