Update app.py
Browse files
app.py
CHANGED
|
@@ -2,62 +2,44 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-small")
|
| 8 |
-
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 9 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
payload = {"inputs": prompt}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 20 |
-
except Exception as e:
|
| 21 |
-
return f"🚫 Ошибка подключения: {str(e)}"
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return f"❌ Ошибка парсинга ответа: {str(e)}\nОтвет: {response.text}"
|
| 30 |
|
| 31 |
-
|
| 32 |
-
if
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
elif isinstance(result, dict) and "generated_text" in result:
|
| 35 |
-
|
| 36 |
-
elif isinstance(result, dict) and "error" in result:
|
| 37 |
-
return f"⚠️ Ошибка модели: {result['error']}"
|
| 38 |
else:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
history = history or []
|
| 44 |
-
prompt = f"Q: {user_input}\nA:"
|
| 45 |
-
response = query_huggingface(prompt)
|
| 46 |
-
history.append((user_input, response))
|
| 47 |
return history, history
|
| 48 |
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.Markdown("## 🤖 FlareGPT — чат с моделью Hugging Face")
|
| 53 |
-
|
| 54 |
-
chatbot = gr.Chatbot(height=400)
|
| 55 |
-
message = gr.Textbox(placeholder="Введите сообщение и нажмите Enter...")
|
| 56 |
-
clear = gr.Button("🧹 Очистить")
|
| 57 |
-
|
| 58 |
-
state = gr.State([])
|
| 59 |
-
|
| 60 |
-
message.submit(chat, [message, state], [chatbot, state])
|
| 61 |
-
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 62 |
-
|
| 63 |
-
demo.launch()
|
|
|
|
| 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 |
+
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=headers, json=payload)
|
| 16 |
+
if response.status_code == 200:
|
| 17 |
+
return response.json()
|
| 18 |
+
elif response.status_code == 404:
|
| 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 |
+
input_text = message
|
|
|
|
| 28 |
|
| 29 |
+
result = query({"inputs": input_text})
|
| 30 |
+
if "error" in result:
|
| 31 |
+
output = result["error"]
|
| 32 |
+
elif isinstance(result, list) and "generated_text" in result[0]:
|
| 33 |
+
output = result[0]["generated_text"]
|
| 34 |
elif isinstance(result, dict) and "generated_text" in result:
|
| 35 |
+
output = result["generated_text"]
|
|
|
|
|
|
|
| 36 |
else:
|
| 37 |
+
output = str(result)
|
|
|
|
| 38 |
|
| 39 |
+
history[-1] = (message, output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return history, history
|
| 41 |
|
| 42 |
+
chat_ui = gr.ChatInterface(fn=chat, title="FlareGPT", theme="soft")
|
| 43 |
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
chat_ui.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|