|
import os |
|
import gradio as gr |
|
import requests |
|
|
|
HF_TOKEN = os.getenv("HF_API_TOKEN") |
|
MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-base") |
|
|
|
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}" |
|
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} |
|
|
|
def query(payload): |
|
response = requests.post(API_URL, headers=HEADERS, json=payload) |
|
if response.status_code != 200: |
|
return f"❌ Ошибка API: {response.status_code} {response.text}" |
|
return response.json() |
|
|
|
def chat(message, history): |
|
if history is None: |
|
history = [] |
|
|
|
history.append((message, "")) |
|
|
|
response = query({"inputs": message}) |
|
|
|
|
|
output = "" |
|
if isinstance(response, dict): |
|
if "error" in response: |
|
output = response["error"] |
|
elif "generated_text" in response: |
|
output = response["generated_text"] |
|
elif isinstance(response.get("choices"), list) and len(response["choices"]) > 0: |
|
output = response["choices"][0].get("message", {}).get("content", "") |
|
else: |
|
output = str(response) |
|
elif isinstance(response, list) and len(response) > 0 and "generated_text" in response[0]: |
|
output = response[0]["generated_text"] |
|
else: |
|
output = str(response) |
|
|
|
|
|
if not isinstance(output, str): |
|
output = str(output) |
|
|
|
|
|
history[-1] = (message, output) |
|
return history, history |
|
|
|
chat_ui = gr.ChatInterface(fn=chat, title="FlareGPT") |
|
|
|
if __name__ == "__main__": |
|
chat_ui.launch() |
|
|