File size: 12,207 Bytes
04c7efc
 
 
 
 
 
 
7c79299
 
a769262
 
7c79299
 
 
 
a769262
 
7c79299
 
 
 
 
a769262
 
 
7c79299
 
 
 
a769262
 
 
7c79299
 
 
 
a769262
7c79299
 
 
 
 
 
a769262
7c79299
 
 
 
 
 
a769262
7c79299
 
 
 
 
 
a769262
 
 
7c79299
04c7efc
 
 
7c79299
04c7efc
7c79299
 
 
 
04c7efc
7c79299
 
 
a769262
7c79299
 
a769262
 
7c79299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a769262
04c7efc
7c79299
 
 
 
 
 
04c7efc
7c79299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04c7efc
 
7c79299
 
04c7efc
 
 
 
7c79299
 
a769262
04c7efc
7c79299
a769262
7c79299
a769262
7c79299
 
a769262
7c79299
a769262
7c79299
a769262
 
 
7c79299
a769262
 
04c7efc
7c79299
 
 
 
 
04c7efc
7c79299
 
 
 
 
 
 
 
 
 
 
e5c4a6a
 
7c79299
 
 
 
 
 
 
e5c4a6a
 
 
7c79299
e5c4a6a
 
 
04c7efc
7c79299
 
 
 
 
 
 
 
 
 
 
 
a769262
7c79299
 
 
04c7efc
7c79299
04c7efc
 
7c79299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04c7efc
 
7c79299
04c7efc
7c79299
a769262
7c79299
 
 
 
 
04c7efc
 
 
7c79299
 
 
 
a769262
7c79299
 
 
 
 
a769262
04c7efc
7c79299
 
 
04c7efc
 
7c79299
 
 
 
 
04c7efc
 
7c79299
 
 
04c7efc
7c79299
 
 
 
 
 
a769262
 
04c7efc
7c79299
 
 
 
 
04c7efc
7c79299
04c7efc
7c79299
 
 
 
 
e5c4a6a
 
7c79299
 
 
 
 
 
 
 
 
04c7efc
7c79299
 
 
 
 
 
 
04c7efc
7c79299
 
04c7efc
7c79299
 
 
a769262
7c79299
a769262
7c79299
 
 
 
 
 
 
a769262
7c79299
a769262
7c79299
 
 
 
a769262
 
 
7c79299
04c7efc
 
 
 
 
 
7c79299
04c7efc
 
 
 
 
a769262
7c79299
a769262
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import gradio as gr
import requests
import os
import json
from typing import List, Dict, Optional
import time

# Curated selection of advanced AI models for general users
ADVANCED_MODELS = {
    "meta-llama/Llama-3.3-70B-Instruct": {
        "provider": "Cerebras",
        "display_name": "Llama 3.3 70B (Ultra Fast)",
        "description": "Meta's latest and most capable model, optimized for speed",
        "category": "General Purpose",
        "endpoint": "https://router.huggingface.co/v1/chat/completions"
    },
    "deepseek-ai/DeepSeek-R1": {
        "provider": "Groq", 
        "display_name": "DeepSeek R1 (Reasoning)",
        "description": "Advanced reasoning model for complex problem solving",
        "category": "Reasoning & Analysis",
        "endpoint": "https://router.huggingface.co/v1/chat/completions"
    },
    "meta-llama/Meta-Llama-3.1-405B-Instruct": {
        "provider": "SambaNova",
        "display_name": "Llama 3.1 405B (Most Powerful)",
        "description": "Meta's largest and most capable language model",
        "category": "Expert Level",
        "endpoint": "https://router.huggingface.co/v1/chat/completions"
    },
    "meta-llama/Meta-Llama-3-70B-Instruct": {
        "provider": "Together",
        "display_name": "Llama 3 70B (Balanced)",
        "description": "Excellent balance of capability and speed",
        "category": "General Purpose", 
        "endpoint": "https://router.huggingface.co/v1/chat/completions"
    },
    "cohere/command-r-plus": {
        "provider": "Cohere",
        "display_name": "Command R+ (Enterprise)",
        "description": "Enterprise-grade model for professional use",
        "category": "Business & Professional",
        "endpoint": "https://router.huggingface.co/v1/chat/completions"
    },
    "Qwen/Qwen2.5-72B-Instruct": {
        "provider": "Novita",
        "display_name": "Qwen 2.5 72B (Multilingual)",
        "description": "Excellent for multiple languages and coding",
        "category": "Multilingual & Code",
        "endpoint": "https://router.huggingface.co/v1/chat/completions"
    },
    "mistralai/Mixtral-8x7B-Instruct-v0.1": {
        "provider": "Nebius",
        "display_name": "Mixtral 8x7B (Efficient)",
        "description": "Fast and efficient for everyday tasks",
        "category": "Daily Tasks",
        "endpoint": "https://router.huggingface.co/v1/chat/completions"
    }
}

class AIChat:
    def __init__(self):
        self.hf_token = os.getenv("HF_TOKEN")
        if not self.hf_token:
            raise ValueError("HF_TOKEN environment variable is required")
        
        self.headers = {
            "Authorization": f"Bearer {self.hf_token}",
            "Content-Type": "application/json"
        }
        
    def send_message(self, model_id: str, message: str, conversation_history: List = None) -> Dict:
        """Send a chat message to the selected AI model"""
        if model_id not in ADVANCED_MODELS:
            return {
                "success": False,
                "error": "Selected model is not available"
            }
        
        model_info = ADVANCED_MODELS[model_id]
        
        # Build conversation with history
        messages = []
        if conversation_history:
            messages.extend(conversation_history)
        messages.append({"role": "user", "content": message})
        
        payload = {
            "model": model_id,
            "messages": messages,
            "max_tokens": 1000,
            "temperature": 0.7,
            "stream": False
        }
        
        try:
            response = requests.post(
                model_info["endpoint"], 
                headers=self.headers, 
                json=payload, 
                timeout=60
            )
            
            if response.status_code == 200:
                result = response.json()
                if "choices" in result and len(result["choices"]) > 0:
                    ai_response = result["choices"][0]["message"]["content"]
                    return {
                        "success": True,
                        "response": ai_response,
                        "model": model_info["display_name"],
                        "provider": model_info["provider"]
                    }
                else:
                    return {
                        "success": False,
                        "error": "No response generated"
                    }
            else:
                return {
                    "success": False,
                    "error": f"API Error: {response.status_code} - {response.text}"
                }
                
        except Exception as e:
            return {
                "success": False,
                "error": f"Connection error: {str(e)}"
            }

def create_chat_interface():
    try:
        chat_ai = AIChat()
    except ValueError as e:
        # Create error interface
        with gr.Blocks(title="❌ Setup Required") as demo:
            gr.Markdown(f"""
            # ❌ Setup Required
            
            **{str(e)}**
            
            Please set the `HF_TOKEN` environment variable with your HuggingFace token.
            
            Get your token at: https://huggingface.co/settings/tokens
            """)
        return demo
    
    # Create model choices for dropdown
    model_choices = [
        (f"🚀 {info['display_name']} - {info['description']}", model_id) 
        for model_id, info in ADVANCED_MODELS.items()
    ]
    
    def chat_with_ai(message, history, selected_model):
        """Handle chat conversation"""
        if not message.strip():
            return history, ""
        
        if not selected_model:
            history.append([message, "❌ Please select an AI model first"])
            return history, ""
        
        # Convert gradio history to API format
        conversation_history = []
        for user_msg, ai_msg in history:
            if user_msg and ai_msg:
                conversation_history.append({"role": "user", "content": user_msg})
                conversation_history.append({"role": "assistant", "content": ai_msg})
        
        # Send message to AI
        result = chat_ai.send_message(selected_model, message, conversation_history)
        
        if result["success"]:
            # Add the new conversation to history
            history.append([message, result["response"]])
            return history, ""
        else:
            # Add error message to history
            history.append([message, f"❌ Error: {result['error']}"])
            return history, ""
    
    def clear_chat():
        """Clear the chat history"""
        return [], ""
    
    def get_model_info(selected_model):
        """Get information about the selected model"""
        if not selected_model or selected_model not in ADVANCED_MODELS:
            return "Select a model to see details"
        
        info = ADVANCED_MODELS[selected_model]
        return f"""
**🤖 {info['display_name']}**

**Provider:** {info['provider']}  
**Category:** {info['category']}  
**Description:** {info['description']}

Ready to chat! Type your message below.
"""
    
    # Create the interface
    with gr.Blocks(
        title="🤖 Chat with Advanced AI Models", 
        theme=gr.themes.Soft(),
        css="""
        .chat-container {
            max-width: 1000px;
            margin: 0 auto;
        }
        .model-info {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 15px;
            border-radius: 10px;
            margin: 10px 0;
        }
        """
    ) as demo:
        
        gr.Markdown("""
        # 🤖 Chat with Advanced AI Models
        
        **Experience the latest AI technology!** Choose from powerful models and start chatting instantly.
        
        ✨ **What you can do:**
        - Ask questions and get intelligent answers
        - Get help with writing, analysis, and creative tasks  
        - Solve problems and get explanations
        - Have natural conversations
        
        """)
        
        with gr.Row():
            # Left column - Model selection
            with gr.Column(scale=1):
                gr.Markdown("### 🎯 Choose Your AI")
                
                model_selector = gr.Dropdown(
                    choices=model_choices,
                    label="Select AI Model",
                    info="Each model has different strengths",
                    interactive=True
                )
                
                model_info_display = gr.Markdown(
                    "Select a model to see details",
                    elem_classes=["model-info"]
                )
                
                # Update model info when selection changes
                model_selector.change(
                    get_model_info,
                    inputs=model_selector,
                    outputs=model_info_display
                )
            
            # Right column - Chat interface  
            with gr.Column(scale=2):
                gr.Markdown("### 💬 Chat Interface")
                
                chatbot = gr.Chatbot(
                    label="Conversation",
                    height=400,
                    show_label=False,
                    container=True,
                    elem_classes=["chat-container"]
                )
                
                with gr.Row():
                    message_input = gr.Textbox(
                        placeholder="Type your message here...",
                        label="Your Message",
                        scale=4,
                        lines=1
                    )
                    send_btn = gr.Button("Send 📤", variant="primary", scale=1)
                
                with gr.Row():
                    clear_btn = gr.Button("Clear Chat 🗑️", variant="secondary")
        
        # Chat functionality
        def submit_message(message, history, model):
            new_history, cleared_input = chat_with_ai(message, history, model)
            return new_history, ""  # Return updated history and clear the input
        
        # Send message on button click or enter
        send_btn.click(
            submit_message,
            inputs=[message_input, chatbot, model_selector],
            outputs=[chatbot, message_input]
        ).then(
            lambda: "", outputs=message_input  # Clear input after sending
        )
        
        message_input.submit(
            submit_message,
            inputs=[message_input, chatbot, model_selector], 
            outputs=[chatbot, message_input]
        ).then(
            lambda: "", outputs=message_input  # Clear input after sending
        )
        
        # Clear chat
        clear_btn.click(clear_chat, outputs=[chatbot, message_input])
        
        # Footer
        gr.Markdown("""
        ---
        
        ## 🚀 **Featured AI Models:**
        
        - **🚀 Ultra Fast**: Llama 3.3 70B on Cerebras chips
        - **🧠 Reasoning**: DeepSeek R1 for complex problem solving  
        - **💪 Most Powerful**: Llama 3.1 405B for expert tasks
        - **⚖️ Balanced**: Llama 3 70B for everyday use
        - **💼 Enterprise**: Command R+ for professional work
        - **🌍 Multilingual**: Qwen 2.5 72B for global communication
        - **⚡ Efficient**: Mixtral 8x7B for quick responses
        
        ## 💡 **Tips for Better Conversations:**
        
        - Be specific about what you want
        - Ask follow-up questions for deeper insights
        - Try different models for different types of tasks
        - Use clear, natural language
        
        ---
        
        *Powered by HuggingFace Inference Providers* 🤗
        """)
    
    return demo

if __name__ == "__main__":
    try:
        demo = create_chat_interface()
        demo.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False
        )
    except Exception as e:
        print(f"Error starting chat application: {e}")
        print("Please ensure HF_TOKEN environment variable is set.")