Spaces:
Sleeping
Sleeping
File size: 1,745 Bytes
ea286be f6a9f7a ab6f153 ea286be f6a9f7a ab6f153 f6a9f7a 4be99f1 f6a9f7a c4a849b f6a9f7a c4a849b f6a9f7a c4a849b f6a9f7a c4a849b f6a9f7a bc379c4 f6a9f7a ea286be | 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 39 40 41 42 43 44 45 46 47 | import gradio as gr
import requests
import os
# Настройки
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-3B-Instruct"
token = os.getenv("HF_TOKEN")
headers = {"Authorization": f"Bearer {token}"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
def respond(message, history):
# Flare - имя ассистента
prompt = f"System: Ты — Flare, профессиональный ИИ-ассистент. Пиши грамотно на русском.\n"
# Берем только последние 2 сообщения для максимальной стабильности
for user_msg, bot_msg in history[-2:]:
prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n"
prompt += f"User: {message}\nAssistant:"
payload = {
"inputs": prompt,
"parameters": {
"max_new_tokens": 500,
"temperature": 0.7,
"return_full_text": False
}
}
try:
output = query(payload)
# Если API вернул ошибку (например, модель грузится)
if "error" in output:
return f"Flare: Сейчас модель подгружается, подожди 10 секунд. (Ошибка: {output['error']})"
return output[0]['generated_text'].strip()
except Exception as e:
return f"Flare: Ошибка связи. Проверь интернет или токен. ({str(e)})"
# Самый простой интерфейс без стриминга (так надежнее)
demo = gr.ChatInterface(fn=respond)
if __name__ == "__main__":
demo.launch() |