File size: 15,788 Bytes
85785ca
 
bc8e480
 
85785ca
 
 
553d8e0
fe39382
 
 
bc8e480
 
 
fe39382
553d8e0
fe39382
 
 
bc8e480
553d8e0
fe39382
 
 
 
bc8e480
fe39382
 
 
 
 
 
 
 
 
 
 
553d8e0
bc8e480
 
553d8e0
bc8e480
 
 
 
553d8e0
fe39382
bc8e480
553d8e0
bc8e480
 
 
 
 
 
 
 
553d8e0
bc8e480
553d8e0
bc8e480
fe39382
bc8e480
553d8e0
fe39382
bc8e480
 
 
 
553d8e0
bc8e480
 
 
 
553d8e0
 
fe39382
 
 
 
bc8e480
 
 
 
 
 
fe39382
 
 
 
bc8e480
 
fe39382
553d8e0
 
fe39382
 
 
bc8e480
553d8e0
fe39382
553d8e0
fe39382
 
 
 
 
 
 
 
 
 
 
 
 
 
553d8e0
 
fe39382
553d8e0
bc8e480
553d8e0
fe39382
 
 
 
 
 
 
bc8e480
fe39382
 
 
 
 
 
 
 
bc8e480
fe39382
553d8e0
fe39382
85785ca
fe39382
bc8e480
85785ca
bc8e480
 
553d8e0
fe39382
 
bc8e480
553d8e0
bc8e480
fe39382
553d8e0
bc8e480
553d8e0
fe39382
 
bc8e480
fe39382
 
bc8e480
553d8e0
fe39382
 
 
bc8e480
553d8e0
 
bc8e480
 
 
 
 
553d8e0
 
fe39382
 
553d8e0
fe39382
553d8e0
fe39382
bc8e480
fe39382
 
 
bc8e480
fe39382
 
 
 
bc8e480
fe39382
 
 
 
bc8e480
fe39382
 
bc8e480
fe39382
bc8e480
 
fe39382
 
 
bc8e480
fe39382
 
 
bc8e480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe39382
bc8e480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeddc3b
 
bc8e480
 
 
 
 
fe39382
 
bc8e480
 
 
553d8e0
bc8e480
 
 
fe39382
bc8e480
85785ca
bc8e480
 
 
 
 
 
 
 
 
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/usr/bin/env python3
"""
Enhanced Gemini Multi-API - Hybrid Web + API Interface
Anthropic-compatible service with both web interface and API endpoints
"""

import os
import json
import time
import uuid
from datetime import datetime
from typing import List, Dict, Any
from flask import Flask, request, jsonify, render_template_string
import gradio as gr
import google.generativeai as genai

# Initialize Flask app
app = Flask(__name__)

# Configuration
GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY', '')
DEFAULT_MODEL = "gemini-1.5-flash"
MAX_TOKENS = 4096
DEFAULT_TEMPERATURE = 0.7

# Model mapping
MODELS = {
    "claude-3-sonnet-20240229": "gemini-1.5-pro",
    "claude-3-haiku-20240307": "gemini-1.5-flash",
    "claude-3-5-sonnet-20241022": "gemini-1.5-pro",
    "claude-3-5-haiku-20241022": "gemini-1.5-flash",
    "gemini-1.5-pro": "gemini-1.5-pro",
    "gemini-1.5-flash": "gemini-1.5-flash",
    "gemini-1.5-pro-002": "gemini-1.5-pro-002",
    "gemini-1.5-flash-8b": "gemini-1.5-flash-8b",
    "gemini-2.0-flash-exp": "gemini-2.0-flash-exp"
}

class GeminiAPI:
    """Anthropic compatible wrapper for Gemini"""
    
    def __init__(self, api_key: str):
        if api_key:
            genai.configure(api_key=api_key)
        self.api_key = api_key
    
    def chat_completion(self, messages: List[Dict], model: str = DEFAULT_MODEL, **kwargs) -> Dict:
        """Anthropic compatible chat completion"""
        
        if not self.api_key:
            return {
                "error": {
                    "type": "configuration_error",
                    "message": "GEMINI_API_KEY not configured",
                    "code": "CONFIGURATION_ERROR"
                }
            }
        
        gemini_model_name = MODELS.get(model, DEFAULT_MODEL)
        
        # Build prompt
        system_prompt = ""
        user_messages = []
        
        for msg in messages:
            if msg.get('role') == 'system':
                system_prompt = msg.get('content', '')
            elif msg.get('role') == 'user':
                user_messages.append(msg.get('content', ''))
        
        full_prompt = system_prompt + "\\n\\n" if system_prompt else ""
        if user_messages:
            full_prompt += f"Human: {user_messages[-1]}"
        full_prompt += "\\n\\nAssistant:"
        
        try:
            model_instance = genai.GenerativeModel(gemini_model_name)
            
            response = model_instance.generate_content(
                full_prompt,
                generation_config=genai.types.GenerationConfig(
                    temperature=kwargs.get('temperature', DEFAULT_TEMPERATURE),
                    max_output_tokens=kwargs.get('max_tokens', MAX_TOKENS),
                    top_p=kwargs.get('top_p', 0.9),
                    top_k=kwargs.get('top_k', 40)
                )
            )
            
            response_text = response.text
            
            # Calculate usage
            input_tokens = len(full_prompt.split()) * 1.3
            output_tokens = len(response_text.split()) * 1.3
            
            return {
                "id": f"msg_{str(uuid.uuid4())[:8]}",
                "type": "message",
                "role": "assistant",
                "content": [{"type": "text", "text": response_text}],
                "model": model,
                "stop_reason": "end_turn",
                "usage": {
                    "input_tokens": int(input_tokens),
                    "output_tokens": int(output_tokens),
                    "cache_creation_input_tokens": 0,
                    "cache_read_input_tokens": 0
                },
                "created_at": int(time.time())
            }
            
        except Exception as e:
            return {
                "error": {
                    "type": "api_error",
                    "message": str(e),
                    "code": "INTERNAL_ERROR"
                }
            }
    
    def list_models(self) -> Dict:
        """List available models"""
        models = [
            {
                "id": "claude-3-sonnet-20240229",
                "object": "model",
                "owned_by": "google-gemini",
                "name": "claude-3-sonnet-20240229",
                "display_name": "Gemini 1.5 Pro (Claude Compatible)",
                "input_token_limit": 2000000,
                "output_token_limit": 8192
            },
            {
                "id": "claude-3-haiku-20240307",
                "object": "model",
                "owned_by": "google-gemini",
                "name": "claude-3-haiku-20240307",
                "display_name": "Gemini 1.5 Flash (Claude Compatible)",
                "input_token_limit": 2000000,
                "output_token_limit": 8192
            }
        ]
        return {"object": "list", "data": models}

# Global API instance
gemini_api = GeminiAPI(GEMINI_API_KEY)

# Flask API Routes
BASE_PATH = "/v1"

@app.route(f"{BASE_PATH}/models", methods=["GET"])
def list_models():
    """List available models"""
    try:
        models = gemini_api.list_models()
        return jsonify(models)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route(f"{BASE_PATH}/messages", methods=["POST"])
def create_message():
    """Create a message"""
    data = request.get_json()
    if not data:
        return jsonify({"error": "Request body required"}), 400
    
    required_fields = ["model", "messages"]
    for field in required_fields:
        if field not in data:
            return jsonify({"error": f"Missing required field: {field}"}), 400
    
    try:
        response = gemini_api.chat_completion(
            messages=data["messages"],
            model=data["model"],
            max_tokens=data.get("max_tokens", 1024),
            temperature=data.get("temperature", 0.7)
        )
        
        if "error" in response:
            return jsonify(response), 500
        
        return jsonify(response)
        
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route("/health", methods=["GET"])
def health_check():
    """Health check"""
    return jsonify({
        "status": "healthy",
        "service": "Enhanced Gemini Multi-API",
        "timestamp": datetime.now().isoformat(),
        "api_key_configured": bool(GEMINI_API_KEY)
    })

@app.route("/info", methods=["GET"])
def api_info():
    """API information"""
    return jsonify({
        "service": "Enhanced Gemini Multi-API",
        "description": "Anthropic API compatible interface for Gemini models",
        "endpoints": {
            "models": f"{BASE_PATH}/models",
            "messages": f"{BASE_PATH}/messages",
            "health": "/health",
            "info": "/info"
        },
        "web_interface": "/gradio",
        "api_key_required": True
    })

# Web Interface Functions
def api_chat_interface(message, history, model, temperature, max_tokens):
    """API-compatible chat interface"""
    if not GEMINI_API_KEY:
        return "❌ GEMINI_API_KEY not configured. Please set the API key in Space secrets."
    
    # Format messages for API
    messages = []
    if history:
        for user_msg, assistant_msg in history:
            messages.append({"role": "user", "content": user_msg})
            if assistant_msg:
                messages.append({"role": "assistant", "content": assistant_msg})
    
    messages.append({"role": "user", "content": message})
    
    # Call API
    response = gemini_api.chat_completion(
        messages=messages,
        model=model,
        max_tokens=max_tokens,
        temperature=temperature
    )
    
    if "error" in response:
        return f"❌ Error: {response['error']['message']}"
    
    try:
        content = response["content"][0]["text"]
        usage = response.get("usage", {})
        tokens = usage.get("input_tokens", 0) + usage.get("output_tokens", 0)
        
        return f"{content}\\n\\n---\\nπŸ’¬ **Tokens Used**: {tokens}"
    except (KeyError, IndexError):
        return "❌ Error: Unable to parse API response"

def test_api():
    """Test API connection"""
    if not GEMINI_API_KEY:
        return "❌ GEMINI_API_KEY not configured"
    
    test_messages = [{"role": "user", "content": "Hello! Test API connection."}]
    response = gemini_api.chat_completion(
        messages=test_messages,
        model="claude-3-haiku-20240307",
        max_tokens=256,
        temperature=0.7
    )
    
    if "error" in response:
        return f"❌ API Test Failed: {response['error']['message']}"
    else:
        return "βœ… API Connection Successful!\\n\\nTest Response:\\n" + response["content"][0]["text"]

def get_models_list():
    """Get available models for interface"""
    if not GEMINI_API_KEY:
        return "❌ GEMINI_API_KEY not configured"
    
    try:
        models_response = gemini_api.list_models()
        models = models_response.get("data", [])
        return "\\n".join([f"β€’ **{model['id']}** - {model['display_name']}" for model in models])
    except Exception as e:
        return f"❌ Error: {str(e)}"

# Gradio Interface
def create_gradio_interface():
    """Create the web interface"""
    
    with gr.Blocks(
        title="Enhanced Gemini Multi-API",
        theme=gr.themes.Soft(),
        show_error=True
    ) as demo:
        
        # Header
        gr.HTML("""
        <div style="text-align: center; padding: 2rem; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 15px; margin-bottom: 2rem;">
            <h1>πŸš€ Enhanced Gemini Multi-API</h1>
            <p>πŸ€– Anthropic Compatible Interface β€’ 🌐 Full API Support β€’ βœ… Production Ready</p>
            <p><strong>Status:</strong> API Service + Web Interface Deployed!</p>
        </div>
        """)
        
        # API Status Tab
        with gr.Tab("πŸ”§ API Status"):
            gr.HTML("<h3>πŸ”§ API Configuration & Testing</h3>")
            
            with gr.Row():
                test_btn = gr.Button("πŸ§ͺ Test API Connection", variant="primary")
                models_btn = gr.Button("πŸ“‹ Available Models", variant="secondary")
            
            status_output = gr.Textbox(
                label="API Test Result",
                lines=6,
                interactive=False
            )
            
            models_output = gr.Textbox(
                label="Available Models",
                lines=6,
                interactive=False
            )
        
        # Chat Interface Tab
        with gr.Tab("πŸ’¬ Chat Interface"):
            gr.HTML("<h3>πŸ’¬ Chat with Anthropic Compatible API</h3>")
            
            with gr.Row():
                model_dropdown = gr.Dropdown(
                    choices=list(MODELS.keys()),
                    value="claude-3-haiku-20240307",
                    label="🧠 Model",
                    info="Anthropic compatible model selection"
                )
                
                temp_slider = gr.Slider(
                    minimum=0.0,
                    maximum=2.0,
                    value=0.7,
                    step=0.1,
                    label="🌑️ Temperature"
                )
                
                max_tokens_slider = gr.Slider(
                    minimum=256,
                    maximum=4096,
                    value=1024,
                    step=256,
                    label="πŸ“ Max Tokens"
                )
            
            chatbot = gr.Chatbot(height=400, label="Chat with Gemini via Anthropic API")
            
            msg = gr.Textbox(
                label="πŸ’­ Your Message",
                placeholder="Type your message here...",
                lines=2
            )
            
            with gr.Row():
                send_btn = gr.Button("πŸš€ Send", variant="primary")
                clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
        
        # API Documentation Tab
        with gr.Tab("πŸ“š API Documentation"):
            gr.HTML("""
            <div style="background: #f8f9fa; padding: 1.5rem; border-radius: 10px; border-left: 4px solid #007bff;">
                <h4>πŸ“š Enhanced Gemini Multi-API Documentation</h4>
                
                <h5>πŸ”§ Endpoints:</h5>
                <ul>
                    <li><code>GET /v1/models</code> - List available models</li>
                    <li><code>POST /v1/messages</code> - Create chat completion</li>
                    <li><code>GET /health</code> - Health check</li>
                    <li><code>GET /info</code> - API information</li>
                </ul>
                
                <h5>πŸ“ Example Usage:</h5>
                <pre><code>curl -X POST https://likhonsheikh-enhanced-gemini-multi-api.hf.space/v1/messages \\
  -H "Content-Type: application/json" \\
  -d '{
    "model": "claude-3-haiku-20240307",
    "messages": [{"role": "user", "content": "Hello!"}],
    "max_tokens": 1024,
    "temperature": 0.7
  }'</code></pre>
                
                <h5>πŸ€– Available Models:</h5>
                <ul>
                    <li><strong>claude-3-haiku-20240307</strong> β†’ Gemini 1.5 Flash</li>
                    <li><strong>claude-3-sonnet-20240229</strong> β†’ Gemini 1.5 Pro</li>
                    <li><strong>claude-3-5-sonnet-20241022</strong> β†’ Gemini 1.5 Pro</li>
                    <li><strong>claude-3-5-haiku-20241022</strong> β†’ Gemini 1.5 Flash</li>
                </ul>
                
                <p><strong>Status:</strong> βœ… Full Anthropic API Compatibility Deployed!</p>
                <p><strong>Updated:</strong> 2025-11-14 04:17:24</p>
            </div>
            """)
        
        # Event handlers
        test_btn.click(test_api, outputs=[status_output])
        models_btn.click(get_models_list, outputs=[models_output])
        
        def user(user_message, history):
            return "", history + [(user_message, None)]
        
        def bot(history, model, temperature, max_tokens):
            if not history:
                return history
            
            user_message, _ = history[-1]
            bot_message = api_chat_interface(user_message, history[:-1], model, temperature, max_tokens)
            history[-1] = (user_message, bot_message)
            return history
        
        msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
            bot, [chatbot, model_dropdown, temp_slider, max_tokens_slider], [chatbot]
        )
        
        send_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
            bot, [chatbot, model_dropdown, temp_slider, max_tokens_slider], [chatbot]
        )
        
        clear_btn.click(lambda: None, outputs=[chatbot], queue=False)
    
    return demo

if __name__ == "__main__":
    # Create Gradio interface
    demo = create_gradio_interface()
    
    # Start both Flask API and Gradio interface
    port = int(os.environ.get("PORT", 7860))
    
    if not GEMINI_API_KEY:
        print("⚠️  GEMINI_API_KEY not configured - API functionality will be limited")
    else:
        print("βœ… GEMINI_API_KEY configured - Full functionality available")
    
    print(f"πŸš€ Enhanced Gemini Multi-API Service starting on port {port}")
    print(f"🌐 Web Interface: http://localhost:{port}/gradio")
    print(f"πŸ“– API Documentation: http://localhost:{port}/info")
    print(f"❀️  Health Check: http://localhost:{port}/health")
    print(f"πŸ€– API Endpoint: http://localhost:{port}/v1/messages")
    
    # Launch with both Flask and Gradio
    demo.launch(
        server_name="0.0.0.0",
        server_port=port,
        share=False,
        show_error=True,
        debug=False,
        quiet=True
    )