Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from huggingface_hub import InferenceClient | |
| # Load Hugging Face token from secrets (set as HF_TOKEN) | |
| hf_token = os.getenv("HF_TOKEN") | |
| # Arabic Chat model client | |
| client = InferenceClient("sambanovasystems/SambaLingo-Arabic-Chat", token=hf_token) | |
| # Define the response function | |
| def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p): | |
| prompt = system_message.strip() + "\n" | |
| for user_msg, bot_msg in history: | |
| if user_msg: | |
| prompt += f"أنت: {user_msg}\n" | |
| if bot_msg: | |
| prompt += f"المساعد: {bot_msg}\n" | |
| prompt += f"أنت: {message}\nالمساعد:" | |
| # Generate response | |
| response = client.text_generation( | |
| prompt=prompt, | |
| max_new_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ) | |
| return response.strip() | |
| # Gradio chat UI | |
| demo = gr.ChatInterface( | |
| fn=respond, | |
| additional_inputs=[ | |
| gr.Textbox(value="أنت مساعد ذكي ودود يتحدث اللغة العربية.", label="رسالة النظام"), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="عدد الرموز القصوى"), | |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.8, step=0.1, label="درجة العشوائية (Temperature)"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (النواة)"), | |
| ], | |
| title="روبوت دردشة باللغة العربية" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |