File size: 3,212 Bytes
0083f4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import requests
from flask import Flask, request, Response, stream_with_context, render_template_string

app = Flask(__name__)

# 🔐 हगिंग फेस 'Secrets' से आपका सुरक्षित डेटा उठाना
API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY") 
BASE_URL = os.environ.get("BASE_URL")
MODEL_ID = os.environ.get("MODEL_ID")

# URL को सुरक्षित बनाना (अगर BASE_URL में अंत में /chat/completions नहीं है तो उसे जोड़ना)
if BASE_URL and not BASE_URL.endswith("/chat/completions"):
    INVOKE_URL = f"{BASE_URL.rstrip('/')}/chat/completions"
else:
    INVOKE_URL = BASE_URL

@app.route('/')
def home():
    try:
        with open('index.html', 'r', encoding='utf-8') as f:
            html_content = f.read()
        return render_template_string(html_content)
    except Exception as e:
        return f"index.html file missing! Error: {str(e)}"

@app.route('/api/chat', methods=['POST'])
def chat():
    data = request.get_json() or {}
    user_message = data.get("message", "")
    attachments = data.get("attachments", []) 

    # 🎨 मल्टीमोडल कंटेंट एरे तैयार करना
    content_payload = []
    
    # 1. टेक्स्ट इनपुट को जोड़ना
    if user_message.strip():
        content_payload.append({"type": "text", "text": user_message})
        
    # 2. इमेज या ऑडियो (Base64) को जोड़ना
    for att in attachments:
        att_type = att.get("type")
        b64_data = att.get("data")
        
        if att_type == "image":
            content_payload.append({
                "type": "image_url", 
                "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}
            })
        elif att_type == "audio":
            content_payload.append({
                "type": "input_audio", 
                "input_audio": {"data": b64_data, "format": "wav"}
            })

    # डिफ़ॉल्ट हैंडलिंग
    if not content_payload:
        content_payload.append({"type": "text", "text": "Hello"})

    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Accept": "text/event-stream"
    }

    # डायनामिक मॉडल आईडी का उपयोग
    payload = {
        "model": MODEL_ID,
        "messages": [{"role": "user", "content": content_payload}],
        "max_tokens": 2048,
        "temperature": 0.20,
        "top_p": 0.70,
        "stream": True
    }

    # API पर स्ट्रीमिंग रिक्वेस्ट भेजना
    response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)

    def generate():
        for line in response.iter_lines():
            if line:
                decoded_line = line.decode("utf-8")
                if decoded_line.startswith("data: "):
                    yield decoded_line + "\n\n"
                    
    return Response(stream_with_context(generate()), mimetype='text/event-stream')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)