import gradio as gr import requests import json from typing import List, Dict, Any, Optional import os import time # Configuration API_BASE_URL = os.getenv("API_BASE_URL", "http://localhost:8000") API_HEALTH_URL = f"{API_BASE_URL}/health" API_CHAT_URL = f"{API_BASE_URL}/api/chat" API_INFO_URL = f"{API_BASE_URL}/model/info" def check_api_connection() -> Dict[str, Any]: """Check if the model API server is running.""" try: response = requests.get(API_HEALTH_URL, timeout=5) if response.status_code == 200: return response.json() else: return {"status": "error", "message": f"API returned status {response.status_code}"} except requests.exceptions.RequestException as e: return {"status": "error", "message": f"Connection failed: {str(e)}"} def chat_with_api(message: str, history: List[Dict[str, str]], language: str = "python", temperature: float = 0.7) -> Dict[str, Any]: """Chat function that calls the model API.""" try: # Check API connection first health_status = check_api_connection() if health_status.get("status") != "healthy": return { "choices": [{"message": {"content": f"āŒ API Server Error: {health_status.get('message', 'Unknown error')}\n\nšŸ’” Make sure the model server is running:\n```bash\npython model_server.py\n```"}}], "history": history } payload = { "message": message, "history": history, "language": language, "temperature": temperature } response = requests.post( API_CHAT_URL, json=payload, headers={"Content-Type": "application/json"}, timeout=60 ) if response.status_code == 200: return response.json() else: return { "choices": [{"message": {"content": f"API Error: {response.status_code} - {response.text}"}}], "history": history } except requests.exceptions.RequestException as e: return { "choices": [{"message": {"content": f"Connection error: {str(e)}"}}], "history": history } except Exception as e: return { "choices": [{"message": {"content": f"Error: {str(e)}"}}], "history": history } def get_model_info_api() -> Dict[str, Any]: """Get model information from the API.""" try: response = requests.get(API_INFO_URL, timeout=5) if response.status_code == 200: return response.json() else: return {"error": f"Failed to get model info: {response.status_code}"} except Exception as e: return {"error": f"Failed to get model info: {str(e)}"} def create_demo(): """Create the Gradio demo interface.""" with gr.Blocks( title="AI Coder - 5B Parameter Chatbot (API)", description="Powered by a 5B parameter language model via API server", theme=gr.themes.Soft(), css=""" .container {max-width: 1200px !important;} .header {text-align: center; padding: 20px;} .header h1 {color: #2d3748; margin-bottom: 10px;} .header a {color: #3182ce; text-decoration: none; font-weight: bold;} .header a:hover {text-decoration: underline;} .status-indicator {padding: 10px; border-radius: 5px; margin: 10px 0;} .status-online {background-color: #d4edda; color: #155724;} .status-offline {background-color: #f8d7da; color: #721c24;} .coding-section {background: #f7fafc; border-radius: 8px; padding: 15px; margin: 10px 0;} """ ) as demo: # Header gr.HTML("""

šŸ¤– AI Coder - API Client

AI chatbot with coding features powered by a 5B parameter model via API

Built with anycoder

""") # Status indicator status_display = gr.HTML() def update_status(): status = check_api_connection() if status.get("status") == "healthy": return f"""
āœ… API Server: Online - Model: {status.get('model_name', 'Unknown')}
""" else: return f"""
āŒ API Server: Offline - {status.get('message', 'Unknown error')}
""" # Main chat interface with gr.Row(): # Left column - Chat with gr.Column(scale=3): chatbot = gr.Chatbot( label="AI Coding Assistant", height=600, type="messages", avatar_images=(None, "šŸ¤–"), show_copy_button=True ) with gr.Row(): msg = gr.Textbox( placeholder="Ask me to code something, debug code, or explain programming concepts...", lines=3, scale=4 ) send_btn = gr.Button("Send", variant="primary", scale=1) with gr.Row(): clear_btn = gr.Button("Clear Chat", variant="secondary") # Right column - Controls with gr.Column(scale=1): gr.Markdown("### šŸ› ļø Settings") language = gr.Dropdown( choices=[ "python", "javascript", "java", "cpp", "c", "go", "rust", "typescript", "php", "ruby", "swift", "kotlin", "sql", "html", "css", "bash", "powershell" ], value="python", label="Programming Language" ) temperature = gr.Slider( minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Creativity (Temperature)" ) # API Status info with gr.Accordion("šŸ”— API Status", open=True): status_text = gr.Markdown() with gr.Accordion("šŸŽÆ Quick Prompts", open=False): gr.Examples( examples=[ "Write a Python function to reverse a linked list", "Create a React component for a login form", "Debug this JavaScript code: [paste code]", "Explain Big O notation with examples", "Create a binary search algorithm in C++" ], inputs=msg, examples_per_page=3 ) with gr.Accordion("ā„¹ļø API Info", open=False): api_info = gr.Markdown() def get_api_info(): info = get_model_info_api() if "error" not in info: return f""" **Model:** {info.get('model_name', 'Unknown')} **Parameters:** {info.get('parameter_count', 'Unknown')} **Max Length:** {info.get('max_length', 'Unknown'):,} tokens **Device:** {info.get('device', 'Unknown')} **Status:** {'āœ… Loaded' if info.get('is_loaded') else 'ā³ Loading...'} **Vocab Size:** {info.get('vocab_size', 'Unknown'):,} """ else: return f"āŒ {info['error']}" api_info.value = get_api_info() # Event handlers def user(user_message, history): return "", history + [{"role": "user", "content": user_message}] def bot(history, selected_language, temp): if not history: return history last_message = history[-1]["content"] result = chat_with_api(last_message, history[:-1], selected_language, temp) return result["history"] # Wire up events msg.submit( user, [msg, chatbot], [msg, chatbot], queue=False ).then( bot, [chatbot, language, temperature], chatbot ) send_btn.click( user, [msg, chatbot], [msg, chatbot], queue=False ).then( bot, [chatbot, language, temperature], chatbot ) clear_btn.click( lambda: [{"role": "assistant", "content": "Hello! I'm your AI coding assistant. I can help you with Python, JavaScript, Java, C++, and many other programming languages. What would you like to code today?"}], outputs=[chatbot] ) # Update status periodically def update_all_status(): status_html = update_status() api_info_text = get_api_info() return status_html, api_info_text # Initial status update status_display.value = update_status() # Load initial message chatbot.value = [{"role": "assistant", "content": "Hello! I'm your AI coding assistant powered by a 5B parameter language model via API. I can help you with Python, JavaScript, Java, C++, and many other programming languages. What would you like to code today?"}] return demo if __name__ == "__main__": demo = create_demo() demo.launch( server_name="0.0.0.0", server_port=7860, show_error=True, share=False, debug=True, mcp_server=True )