Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| import gradio as gr | |
| # Gemini API key | |
| gemini_api_key = os.getenv("GEMINI_API_KEY") | |
| if gemini_api_key is None: | |
| raise ValueError("⚠️ GEMINI_API_KEY тохируулаагүй байна!") | |
| # Gemini response function | |
| def gemini_response(prompt): | |
| try: | |
| url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={gemini_api_key}" | |
| headers = {"Content-Type": "application/json"} | |
| data = {"contents": [{"parts": [{"text": prompt}]}]} | |
| res = requests.post(url, headers=headers, json=data) | |
| res_json = res.json() | |
| return res_json["candidates"][0]["content"]["parts"][0]["text"] | |
| except Exception as e: | |
| return f"⚠️ Gemini error: {e}" | |
| # Chat function with history | |
| def chat(prompt, history=[]): | |
| reply = gemini_response(prompt) | |
| history.append((prompt, reply)) | |
| return history, history | |
| # Custom CSS for chat design | |
| css = """ | |
| .gradio-container {background-color: #0d1117 !important; color: #e6edf3;} | |
| h1, h3, h4, label {color: #00d9ff !important;} | |
| textarea, input {background-color:#161b22 !important; color:#fff !important; border-radius:10px;} | |
| button {background-color:#00d9ff !important; color:#000 !important; font-weight:bold; border-radius:10px;} | |
| .gradio-chatbot-message.user {background-color:#0f2c54; color:#fff; border-radius:15px; padding:10px;} | |
| .gradio-chatbot-message.bot {background-color:#1f1f1f; color:#00d9ff; border-radius:15px; padding:10px;} | |
| """ | |
| # Gradio interface | |
| with gr.Blocks(css=css, theme=gr.themes.Soft()) as app: | |
| gr.Markdown("## 🚀 ZeppFusion AI — Gemini Chat Only") | |
| chatbot = gr.Chatbot() | |
| msg = gr.Textbox(placeholder="Таны асуулт...") | |
| clear = gr.Button("🗑️ Clear History") | |
| msg.submit(chat, [msg, chatbot], [chatbot, chatbot]) | |
| # Clear button | |
| clear.click(lambda: [], None, chatbot) | |
| if __name__ == "__main__": | |
| app.launch() |