Spaces:
Runtime error

DORINDESTRES commited on
Commit
aaeb012
·
verified ·
1 Parent(s): f0f1835

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -7
app.py CHANGED
@@ -1,18 +1,27 @@
1
  import gradio as gr
2
  import requests
 
3
 
4
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
5
- import os
6
  headers = {"Authorization": f"Bearer {os.environ['HF_TOKEN']}"}
7
 
8
  def chat(prompt, history=[]):
9
- full_prompt = "\n".join([f"User: {msg[0]}\nAI: {msg[1]}" for msg in history])
10
- full_prompt += f"\nUser: {prompt}\nAI:"
11
- response = requests.post(API_URL, headers=headers, json={"inputs": full_prompt})
 
 
12
  try:
13
- answer = response.json()[0]['generated_text'].split("AI:")[-1].strip()
14
- except:
15
- answer = "⚠️ Ошибка при ответе."
 
 
 
 
 
 
 
16
  history.append((prompt, answer))
17
  return history, history
18
 
 
1
  import gradio as gr
2
  import requests
3
+ import os
4
 
5
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
 
6
  headers = {"Authorization": f"Bearer {os.environ['HF_TOKEN']}"}
7
 
8
  def chat(prompt, history=[]):
9
+ full_prompt = ""
10
+ for user_msg, bot_msg in history:
11
+ full_prompt += f"User: {user_msg}\nAI: {bot_msg}\n"
12
+ full_prompt += f"User: {prompt}\nAI:"
13
+
14
  try:
15
+ response = requests.post(API_URL, headers=headers, json={"inputs": full_prompt})
16
+ result = response.json()
17
+ if isinstance(result, list):
18
+ generated_text = result[0]["generated_text"]
19
+ answer = generated_text.split("AI:")[-1].strip()
20
+ else:
21
+ answer = result.get("error", "⚠️ Ошибка от модели.")
22
+ except Exception as e:
23
+ answer = f"⚠️ Ошибка: {str(e)}"
24
+
25
  history.append((prompt, answer))
26
  return history, history
27