Sheelu Katiyar commited on
Commit
0083f4e
·
verified ·
1 Parent(s): 5b13aaf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import requests
4
+ from flask import Flask, request, Response, stream_with_context, render_template_string
5
+
6
+ app = Flask(__name__)
7
+
8
+ # 🔐 हगिंग फेस 'Secrets' से आपका सुरक्षित डेटा उठाना
9
+ API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
10
+ BASE_URL = os.environ.get("BASE_URL")
11
+ MODEL_ID = os.environ.get("MODEL_ID")
12
+
13
+ # URL को सुरक्षित बनाना (अगर BASE_URL में अंत में /chat/completions नहीं है तो उसे जोड़ना)
14
+ if BASE_URL and not BASE_URL.endswith("/chat/completions"):
15
+ INVOKE_URL = f"{BASE_URL.rstrip('/')}/chat/completions"
16
+ else:
17
+ INVOKE_URL = BASE_URL
18
+
19
+ @app.route('/')
20
+ def home():
21
+ try:
22
+ with open('index.html', 'r', encoding='utf-8') as f:
23
+ html_content = f.read()
24
+ return render_template_string(html_content)
25
+ except Exception as e:
26
+ return f"index.html file missing! Error: {str(e)}"
27
+
28
+ @app.route('/api/chat', methods=['POST'])
29
+ def chat():
30
+ data = request.get_json() or {}
31
+ user_message = data.get("message", "")
32
+ attachments = data.get("attachments", [])
33
+
34
+ # 🎨 मल्टीमोडल कंटेंट एरे तैयार करना
35
+ content_payload = []
36
+
37
+ # 1. टेक्स्ट इनपुट को जोड़ना
38
+ if user_message.strip():
39
+ content_payload.append({"type": "text", "text": user_message})
40
+
41
+ # 2. इमेज या ऑडियो (Base64) को जोड़ना
42
+ for att in attachments:
43
+ att_type = att.get("type")
44
+ b64_data = att.get("data")
45
+
46
+ if att_type == "image":
47
+ content_payload.append({
48
+ "type": "image_url",
49
+ "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}
50
+ })
51
+ elif att_type == "audio":
52
+ content_payload.append({
53
+ "type": "input_audio",
54
+ "input_audio": {"data": b64_data, "format": "wav"}
55
+ })
56
+
57
+ # डिफ़ॉल्ट हैंडलिंग
58
+ if not content_payload:
59
+ content_payload.append({"type": "text", "text": "Hello"})
60
+
61
+ headers = {
62
+ "Authorization": f"Bearer {API_KEY}",
63
+ "Accept": "text/event-stream"
64
+ }
65
+
66
+ # डायनामिक मॉडल आईडी का उपयोग
67
+ payload = {
68
+ "model": MODEL_ID,
69
+ "messages": [{"role": "user", "content": content_payload}],
70
+ "max_tokens": 2048,
71
+ "temperature": 0.20,
72
+ "top_p": 0.70,
73
+ "stream": True
74
+ }
75
+
76
+ # API पर स्ट्रीमिंग रिक्वेस्ट भेजना
77
+ response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
78
+
79
+ def generate():
80
+ for line in response.iter_lines():
81
+ if line:
82
+ decoded_line = line.decode("utf-8")
83
+ if decoded_line.startswith("data: "):
84
+ yield decoded_line + "\n\n"
85
+
86
+ return Response(stream_with_context(generate()), mimetype='text/event-stream')
87
+
88
+ if __name__ == '__main__':
89
+ app.run(host='0.0.0.0', port=7860)