File size: 10,444 Bytes
6a6c658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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("""
        <div class="header">
            <h1>πŸ€– AI Coder - API Client</h1>
            <p>AI chatbot with coding features powered by a 5B parameter model via API</p>
            <p>Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">anycoder</a></p>
        </div>
        """)
        
        # Status indicator
        status_display = gr.HTML()
        
        def update_status():
            status = check_api_connection()
            if status.get("status") == "healthy":
                return f"""
                <div class="status-indicator status-online">
                    βœ… API Server: Online - Model: {status.get('model_name', 'Unknown')}
                </div>
                """
            else:
                return f"""
                <div class="status-indicator status-offline">
                    ❌ API Server: Offline - {status.get('message', 'Unknown error')}
                </div>
                """
        
        # 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
    )