Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 = "
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|