HaveAI commited on
Commit
8ffe745
·
verified ·
1 Parent(s): 9f26ce1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -48
app.py CHANGED
@@ -2,62 +2,44 @@ import os
2
  import gradio as gr
3
  import requests
4
 
5
- # Загружаем токен и имя модели из secrets
6
- API_TOKEN = os.getenv("HF_API_TOKEN")
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
- def query_huggingface(prompt):
13
- if not API_TOKEN:
14
- return "❌ Токен HF_API_TOKEN не задан! Добавь его в Secrets."
15
-
16
- payload = {"inputs": prompt}
17
 
18
- try:
19
- response = requests.post(API_URL, headers=headers, json=payload)
20
- except Exception as e:
21
- return f"🚫 Ошибка подключения: {str(e)}"
22
 
23
- if response.status_code != 200:
24
- return f"❌ Ошибка API: {response.status_code}\n{response.text}"
 
 
 
 
 
 
 
 
25
 
26
- try:
27
- result = response.json()
28
- except Exception as e:
29
- return f"❌ Ошибка парсинга ответа: {str(e)}\nОтвет: {response.text}"
30
 
31
- # Разбор разных форматов
32
- if isinstance(result, list) and "generated_text" in result[0]:
33
- return result[0]["generated_text"]
 
 
34
  elif isinstance(result, dict) and "generated_text" in result:
35
- return result["generated_text"]
36
- elif isinstance(result, dict) and "error" in result:
37
- return f"⚠️ Ошибка модели: {result['error']}"
38
  else:
39
- return f"⚠️ Неожиданный формат ответа: {result}"
40
-
41
 
42
- def chat(user_input, history):
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
- # UI через Gradio Blocks
51
- with gr.Blocks() as demo:
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()