Spaces:
Sleeping

aaa / app.py
EhteshamiAI's picture
Update app.py
6b0aeb4 verified
Raw History Blame Contribute Delete
1.56 kB
import gradio as gr
import os
from openai import OpenAI
# توکن از Secrets خوانده می‌شود - این خط درسته
client = OpenAI(
base_url="https://router.huggingface.co/v1",
api_key=os.environ.get("HF_TOKEN", "")
)
def chat_with_ai(message, history):
try:
completion = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3.2-Exp:novita",
messages=[
{
"role": "system",
"content": "You are Ehteshami AI - a helpful Persian assistant. همیشه به فارسی پاسخ بده."
},
{
"role": "user",
"content": message
}
],
max_tokens=500
)
response = completion.choices[0].message.content
return response
except Exception as e:
return f"⚠️ خطا: {str(e)}"
# ساخت رابط چت
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🤖 Ehteshami AI")
gr.Markdown("**دستیار هوشمند فارسی شما**")
chatbot = gr.Chatbot(height=400)
msg = gr.Textbox(label="پیام شما", placeholder="سلام! چطوری می‌تونم کمک کنم؟")
def respond(message, chat_history):
bot_message = chat_with_ai(message, chat_history)
chat_history.append((message, bot_message))
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
demo.launch()