import gradio as gr from litellm import completion import os # تنظیمات TITLE = "🤖 هاب AI شخصی من" AVATAR = "https://i.imgur.com/4z7v3.gif" # لینک آواتار پیش‌فرض - بعداً عوض کن def chat(message, history, model, api_key): if not api_key: return "ابتدا Gemini API Key خود را وارد کنید!", history try: messages = [{"role": m[0], "content": m[1]} for m in history] + [{"role": "user", "content": message}] response = completion( model=model, messages=messages, api_key=api_key ) reply = response.choices[0].message.content history.append((message, reply)) return "", history except Exception as e: return f"خطا: {str(e)}", history def tts(text): if not text: return "هیچ جوابی برای خواندن وجود ندارد." return f"🔊 صدا پخش شد: {text[:150]}..." with gr.Blocks(title=TITLE, theme=gr.themes.Soft()) as demo: gr.Markdown(f"# {TITLE}\n\n**همه مدل‌های هوش مصنوعی در یک صفحه شخصی**") with gr.Row(): with gr.Column(scale=1): gr.Image(AVATAR, label="آواتار من", height=220, interactive=False) api_key = gr.Textbox(label="Gemini API Key", type="password", placeholder="کلید API را اینجا paste کنید") model_choice = gr.Dropdown( choices=["gemini/gemini-1.5-pro", "grok/grok-beta"], value="gemini/gemini-1.5-pro", label="انتخاب مدل هوش مصنوعی" ) avatar_link = gr.Textbox(value=AVATAR, label="لینک آواتار جدید (GIF برای متحرک)") with gr.Column(scale=3): chatbot = gr.Chatbot(height=550, avatar_images=[None, AVATAR], label="چت با هوش مصنوعی") msg = gr.Textbox(label="پیام شما", placeholder="سلام! چطوری؟", lines=2) with gr.Row(): send_btn = gr.Button("📤 ارسال", variant="primary", size="large") speak_btn = gr.Button("🔊 خواندن آخرین جواب") with gr.Row(): music_btn = gr.Button("🎵 ساخت آهنگ") video_btn = gr.Button("🎥 ساخت ویدیو / تصویر") # عملکردها def respond(message, history, model, api_key): if not message.strip(): return "", history _, new_history = chat(message, history, model, api_key) return "", new_history def update_avatar(link): return link send_btn.click(respond, inputs=[msg, chatbot, model_choice, api_key], outputs=[msg, chatbot]) speak_btn.click(lambda hist: tts(hist[-1][1] if hist else ""), inputs=chatbot, outputs=None) music_btn.click(lambda: gr.Markdown("**برو به Suno.ai** برای ساخت آهنگ"), outputs=None) video_btn.click(lambda: gr.Markdown("**برو به Google AI Studio** برای ساخت تصویر/ویدیو"), outputs=None) avatar_link.change(update_avatar, inputs=avatar_link, outputs=None) # برای بروزرسانی آواتار gr.Markdown(""" ### راهنما: - Gemini API Key رایگان از [Google AI Studio](https://aistudio.google.com) بگیر - برای آواتار متحرک: لینک GIF از Imgur یا Giphy بذار - روی گوشی هم عالی کار می‌کنه! """) demo.launch(share=True)