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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -29
app.py CHANGED
@@ -4,12 +4,11 @@ from flask import Flask, request, Response, stream_with_context, render_template
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,7 +27,6 @@ def home():
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,7 +34,7 @@ def chat():
36
  user_message = data.get("message", "")
37
  attachments = data.get("attachments", [])
38
  system_prompt = data.get("system_prompt", "")
39
- history = data.get("history", []) # 👈 यहाँ से हिस्ट्री रिसीव होगी
40
  max_tokens = data.get("max_tokens", 4096)
41
  temperature = data.get("temperature", 0.6)
42
 
@@ -46,36 +44,39 @@ def chat():
46
  if system_prompt.strip():
47
  messages.append({"role": "system", "content": system_prompt})
48
 
49
- # 2. पुरानी हिस्ट्री लोड करना (यहीिसिग था)
50
  for msg in history:
51
- # फ्रंटएंड 'bot' भेजता है, लेकिन मॉडल को 'assistant' चाहिए होता है
52
  role = "assistant" if msg.get("role") == "bot" else "user"
53
  content = msg.get("content", "")
54
  if content:
55
- messages.append({"role": role, "content": content})
56
-
57
- # 3. मल्टीमोडल इनपुट (करंट मैसेज)
58
- content_payload = []
59
- if user_message.strip():
60
- content_payload.append({"type": "text", "text": user_message})
61
-
62
- for att in attachments:
63
- att_type = att.get("type")
64
- b64_data = att.get("data")
65
-
66
- if att_type == "image":
67
- content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
68
- elif att_type in ["audio", "file"]:
69
- content_payload.append({"type": "input_audio", "input_audio": {"data": b64_data, "format": "wav"}})
70
-
71
- if not content_payload:
72
- content_payload.append({"type": "text", "text": "Hello"})
73
-
74
- messages.append({"role": "user", "content": content_payload})
 
 
 
75
 
76
  headers = {
77
  "Authorization": f"Bearer {API_KEY}",
78
- "Accept": "text/event-stream"
 
79
  }
80
 
81
  payload = {
@@ -88,9 +89,12 @@ def chat():
88
  }
89
 
90
  try:
91
- # सुरक्षित स्ट्रीमिंग रिक्वेस्ट
92
  response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
93
 
 
 
 
 
94
  def generate():
95
  for line in response.iter_lines():
96
  if line:
@@ -100,7 +104,9 @@ def chat():
100
 
101
  return Response(stream_with_context(generate()), mimetype='text/event-stream')
102
  except Exception as e:
103
- return Response("Internal Error: Unable to process request securely.", status=500)
 
104
 
105
  if __name__ == '__main__':
 
106
  app.run(host='0.0.0.0', port=7860)
 
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
 
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
  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
  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
  }
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
 
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)