Vedika commited on
Commit
5e89b71
·
verified ·
1 Parent(s): 18db9b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -39
app.py CHANGED
@@ -1,14 +1,15 @@
1
- import os
2
  import requests
3
  from flask import Flask, request, Response, stream_with_context, render_template_string
4
 
5
  app = Flask(__name__)
6
 
7
- # 🔐 100% सुरक्षित: सब कुछ केवल 'Secrets' से कॉल होगा
8
  API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
9
  BASE_URL = os.environ.get("BASE_URL")
10
  MODEL_ID = os.environ.get("MODEL_ID")
11
 
 
12
  INVOKE_URL = ""
13
  if BASE_URL:
14
  if not BASE_URL.endswith("/chat/completions"):
@@ -27,6 +28,7 @@ def home():
27
 
28
  @app.route('/api/chat', methods=['POST'])
29
  def chat():
 
30
  if not API_KEY or not INVOKE_URL or not MODEL_ID:
31
  return Response("Server Error: Configuration secrets are missing.", status=500)
32
 
@@ -34,7 +36,6 @@ def chat():
34
  user_message = data.get("message", "")
35
  attachments = data.get("attachments", [])
36
  system_prompt = data.get("system_prompt", "")
37
- history = data.get("history", [])
38
  max_tokens = data.get("max_tokens", 4096)
39
  temperature = data.get("temperature", 0.6)
40
 
@@ -44,39 +45,28 @@ def chat():
44
  if system_prompt.strip():
45
  messages.append({"role": "system", "content": system_prompt})
46
 
47
- # 2. पुरानी हिस्ट्रोड करा (Safe format में)
48
- for msg in history:
49
- role = "assistant" if msg.get("role") == "bot" else "user"
50
- content = msg.get("content", "")
51
- if content:
52
- messages.append({"role": role, "content": str(content)})
 
 
 
 
 
 
 
53
 
54
- # 3. मल्टीमोडल इनपुट (करंट मैसेज) - ओरिजिनल लॉजिक
55
- if attachments:
56
- # अगर इमेज या फाइल है, तो लिस्ट वाला फॉर्मेट इस्तेमाल होगा
57
- content_payload = []
58
- if user_message.strip():
59
- content_payload.append({"type": "text", "text": user_message})
60
-
61
- for att in attachments:
62
- att_type = att.get("type")
63
- b64_data = att.get("data")
64
-
65
- if att_type == "image":
66
- content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
67
- elif att_type in ["audio", "file"]:
68
- content_payload.append({"type": "input_audio", "input_audio": {"data": b64_data, "format": "wav"}})
69
 
70
- messages.append({"role": "user", "content": content_payload})
71
- else:
72
- # अगर कोई फाइल नहीं है, तो सिंपल टेक्स्ट भेजा जाएगा (यही API क्रैश को रोकेगा)
73
- safe_text = user_message.strip() if user_message.strip() else "Hello"
74
- messages.append({"role": "user", "content": safe_text})
75
 
76
  headers = {
77
  "Authorization": f"Bearer {API_KEY}",
78
- "Accept": "text/event-stream",
79
- "Content-Type": "application/json"
80
  }
81
 
82
  payload = {
@@ -89,12 +79,9 @@ def chat():
89
  }
90
 
91
  try:
 
92
  response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
93
 
94
- # अगर API से कोई एरर आता है, तो वह डॉकर (Hugging Face) के लॉग्स में प्रिंट हो जाएगा
95
- if response.status_code != 200:
96
- print(f"API Error ({response.status_code}): {response.text}", flush=True)
97
-
98
  def generate():
99
  for line in response.iter_lines():
100
  if line:
@@ -104,9 +91,7 @@ def chat():
104
 
105
  return Response(stream_with_context(generate()), mimetype='text/event-stream')
106
  except Exception as e:
107
- print(f"Backend Exception: {str(e)}", flush=True)
108
- return Response("Internal Error: Unable to process request.", status=500)
109
 
110
  if __name__ == '__main__':
111
- # डॉकर स्पेस के लिए पोर्ट 7860
112
- app.run(host='0.0.0.0', port=7860)
 
1
+ Import os
2
  import requests
3
  from flask import Flask, request, Response, stream_with_context, render_template_string
4
 
5
  app = Flask(__name__)
6
 
7
+ # 🔐 100% सुरक्षित: सब कुछ केवल 'Secrets' से कॉल होगा, कोई हार्डकोडेड डेटा नहीं।
8
  API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
9
  BASE_URL = os.environ.get("BASE_URL")
10
  MODEL_ID = os.environ.get("MODEL_ID")
11
 
12
+ # URL को सुरक्षित रूप से बनाना ताकि कोई डिफ़ॉल्ट लिंक न दिखे
13
  INVOKE_URL = ""
14
  if BASE_URL:
15
  if not BASE_URL.endswith("/chat/completions"):
 
28
 
29
  @app.route('/api/chat', methods=['POST'])
30
  def chat():
31
+ # सुरक्षा जांच: अगर सीक्रेट्स सेट नहीं हैं, तो रिक्वेस्ट आगे नहीं जाएगी
32
  if not API_KEY or not INVOKE_URL or not MODEL_ID:
33
  return Response("Server Error: Configuration secrets are missing.", status=500)
34
 
 
36
  user_message = data.get("message", "")
37
  attachments = data.get("attachments", [])
38
  system_prompt = data.get("system_prompt", "")
 
39
  max_tokens = data.get("max_tokens", 4096)
40
  temperature = data.get("temperature", 0.6)
41
 
 
45
  if system_prompt.strip():
46
  messages.append({"role": "system", "content": system_prompt})
47
 
48
+ # 2. मल्टीोड पुट
49
+ content_payload = []
50
+ if user_message.strip():
51
+ content_payload.append({"type": "text", "text": user_message})
52
+
53
+ for att in attachments:
54
+ att_type = att.get("type")
55
+ b64_data = att.get("data")
56
+
57
+ if att_type == "image":
58
+ content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
59
+ elif att_type in ["audio", "file"]:
60
+ content_payload.append({"type": "input_audio", "input_audio": {"data": b64_data, "format": "wav"}})
61
 
62
+ if not content_payload:
63
+ content_payload.append({"type": "text", "text": "Hello"})
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
+ messages.append({"role": "user", "content": content_payload})
 
 
 
 
66
 
67
  headers = {
68
  "Authorization": f"Bearer {API_KEY}",
69
+ "Accept": "text/event-stream"
 
70
  }
71
 
72
  payload = {
 
79
  }
80
 
81
  try:
82
+ # सुरक्षित स्ट्रीमिंग रिक्वेस्ट
83
  response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
84
 
 
 
 
 
85
  def generate():
86
  for line in response.iter_lines():
87
  if line:
 
91
 
92
  return Response(stream_with_context(generate()), mimetype='text/event-stream')
93
  except Exception as e:
94
+ return Response("Internal Error: Unable to process request securely.", status=500)
 
95
 
96
  if __name__ == '__main__':
97
+ app.run(host='0.0.0.0', port=7860)