File size: 1,102 Bytes
b986c5c 2655d77 b986c5c 59fd87d b986c5c 8ffe745 9e594ee 8ffe745 a788b34 9e594ee 2655d77 59fd87d 2655d77 59fd87d 2655d77 a788b34 2655d77 b986c5c 2655d77 cb7d534 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 |
import gradio as gr
import os
import requests
from dotenv import load_dotenv
load_dotenv()
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 chat_fn(message, history):
try:
prompt = f"Answer the question: {message}"
payload = {"inputs": prompt}
response = requests.post(API_URL, headers=HEADERS, json=payload)
result = response.json()
print(result)
if isinstance(result, list) and "generated_text" in result[0]:
return result[0]["generated_text"]
elif isinstance(result, dict) and "generated_text" in result:
return result["generated_text"]
else:
return "❌ Ответ не распознан"
except Exception as e:
return f"❌ Ошибка: {str(e)}"
gr.ChatInterface(
fn=chat_fn,
title="FlareGPT",
retry_btn="🔄 Retry",
undo_btn="↩️ Undo",
clear_btn="🗑️ Clear",
).launch()
|