import gradio as gr from google import genai from google.genai import types # Initialize the client with your API key client = genai.Client(api_key='AIzaSyBZ26ErI0Q5hya4L4u-H3nBnv09AYVBWMA') def query_gemini(message, history): """ Query the Gemini model with file search capabilities and conversation history Args: message (str): The user's current message history (list): The conversation history in messages format Returns: str: The model's response """ if not message.strip(): return "⚠️ يرجى إدخال سؤال" try: # Build the conversation contents for Gemini # Convert Gradio history format to Gemini format contents = [] # Add conversation history for msg in history: # Gradio messages format: {'role': 'user'/'assistant', 'content': 'text'} if msg['role'] == 'user': contents.append(types.Content( role='user', parts=[types.Part(text=msg['content'])] )) elif msg['role'] == 'assistant': contents.append(types.Content( role='model', parts=[types.Part(text=msg['content'])] )) # Add the current message contents.append(types.Content( role='user', parts=[types.Part(text=message)] )) # Generate response with conversation history response = client.models.generate_content( model="gemini-flash-latest", contents=contents, config=types.GenerateContentConfig( tools=[ types.Tool( file_search=types.FileSearch( file_search_store_names=[ "fileSearchStores/sanhouri-dwquoge2cowu", "fileSearchStores/sanhouri1-oq0fg9gnv444" ] ) ) ] ) ) return response.text except Exception as e: return f"❌ حدث خطأ: {str(e)}" # Create the Gradio chatbot interface with gr.Blocks( theme=gr.themes.Soft( primary_hue="blue", secondary_hue="indigo" ), title="تحدث مع السنهوري", css=""" .gradio-container {direction: rtl;} .contain {max-width: 1000px; margin: auto;} .message-wrap {direction: rtl !important;} """ ) as demo: # Header gr.Markdown( """ # 👨‍⚖️ تحدث مع السنهوري ### محادثة تفاعلية مع خبير القانون المدني باستخدام Gemini AI #### 💬 يحفظ النموذج سياق المحادثة للإجابات الأكثر دقة """ ) # Chat interface gr.ChatInterface( fn=query_gemini, type="messages", chatbot=gr.Chatbot( label="محادثة", height=500, rtl=True, show_copy_button=True, avatar_images=(None, "🎓") ), textbox=gr.Textbox( placeholder="اكتب سؤالك عن القانون المدني هنا...", container=False, scale=7, rtl=True ), examples=[ "كيف عالج القانون المدني الجديد التهديد المالي؟", "ما هي شروط صحة العقد في القانون المدني؟", "كيف ينظم القانون المدني المسؤولية التقصيرية؟", "ما هي أركان العقد؟", "اشرح لي مفهوم السبب في العقود", ], ) # Info box gr.Markdown( """ --- **ℹ️ ملاحظة:** هذا المساعد الذكي يبحث في قواعد بيانات القانون المدني (موسوعة السنهوري) للحصول على إجابات دقيقة وموثوقة. **💡 مميزات:** - 🧠 يتذكر سياق المحادثة الكاملة - 🔍 يبحث في قواعد بيانات القانون المدني - 💬 يمكنك طرح أسئلة متتابعة ومترابطة - 📝 يمكنك الرجوع لأي نقطة سابقة في المحادثة **💭 نصيحة:** استخدم أزرار "Retry" لإعادة توليد الإجابة أو "Undo" للتراجع عن آخر رسالة """ ) # Launch the app if __name__ == "__main__": demo.launch( share=True, # Set to True if you want a public link server_name="0.0.0.0" # Allow external access # Port will be auto-assigned if 7860 is in use )