persian-article / app.py
mohammadshahabiy's picture
Update app.py
e349f17 verified
raw
history blame
1.7 kB
import gradio as gr
from huggingface_hub import InferenceClient
# استفاده از Hugging Face Inference API برای GPT-3.5-Turbo
client = InferenceClient("openai/gpt-3.5-turbo")
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p,
):
# ساخت پیام‌ها برای مدل
messages = [{"role": "system", "content": system_message}]
# اضافه کردن تاریخچه چت
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
# تولید پاسخ با استفاده از مدل
response = client.chat_completion(
messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
)
yield response["choices"][0]["message"]["content"]
# ایجاد رابط کاربری با Gradio
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(value="شما یک ربات دوست‌داشتنی هستید.", label="پیام سیستم"),
gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="حداکثر توکن‌های جدید"),
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="دما (Temperature)"),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.9,
step=0.05,
label="Top-p (نمونه‌گیری هسته‌ای)",
),
],
)
# اجرای برنامه
if __name__ == "__main__":
demo.launch()