sreepathi-ravikumar commited on
Commit
307e121
·
verified ·
1 Parent(s): 32be864

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -43
app.py CHANGED
@@ -1,14 +1,20 @@
1
  from flask import Flask, request, Response, jsonify, stream_with_context
2
  from flask_cors import CORS
3
- import os, json, httpx
4
  import config
5
 
6
-
7
  app = Flask(__name__)
8
  CORS(app)
9
 
10
  OPENROUTER_API_KEY = os.getenv("API_KEYS")
11
 
 
 
 
 
 
 
 
12
  @app.get("/health")
13
  def health():
14
  return jsonify({"status": "ready"}), 200
@@ -22,33 +28,40 @@ def _headers():
22
  }
23
 
24
  def _payload(q: str):
25
- data=q.split("%")
26
- content=""
27
- system=""
28
- if data[0]=="Text":
29
- content=data[1]
 
30
  else:
31
- content={
32
- "type": "image_url",
33
- "image_url": {
34
- "url": data[1]
35
- }
36
  }
37
- info=config.config
38
- system=info[0][data[3]]
 
39
 
40
  return {
41
  "model": "meta-llama/llama-4-maverick-17b-128e-instruct",
42
  "stream": True,
43
- "messages": [{"role":"system","content":system},{"role": "user", "content": content}],
 
 
 
44
  "temperature": 0.7,
45
- "top_p":0.9,
46
  "max_tokens": 999,
47
  }
48
 
49
  def stream_openrouter(q: str):
50
  if not OPENROUTER_API_KEY:
51
- yield "data: Error: Missing API key\n\n"
 
 
52
  return
53
 
54
  try:
@@ -71,16 +84,24 @@ def stream_openrouter(q: str):
71
  content = json_data['choices'][0].get('delta', {}).get('content')
72
  print(content)
73
  if content:
74
- yield f"data: {json.dumps(content)}\n\n"
 
 
75
  except json.JSONDecodeError:
76
  continue
77
 
78
- yield "data: [DONE]\n\n"
 
 
79
 
80
  except httpx.HTTPError as e:
81
- yield f"data: Error: {str(e)}\n\n"
 
 
82
  except Exception as e:
83
- yield f"data: Unexpected error: {str(e)}\n\n"
 
 
84
 
85
  @app.post("/ask")
86
  def ask():
@@ -88,12 +109,11 @@ def ask():
88
  q = payload.get("question", "").strip()
89
  lang = payload.get("selectedLanguage", "").strip()
90
  mode = payload.get("selectedMode", "").strip()
91
- prompt=""
92
- q2lang=""
93
  if not q:
94
  q = "Say hello in one short sentence."
95
 
96
- prompt="Text"+"%"+q+"%"+mode+"%"+lang
97
  print(prompt)
98
 
99
  return Response(
@@ -107,26 +127,54 @@ def ask():
107
 
108
  @app.post("/askimage")
109
  def askimage():
110
- payload = request.get_json(silent=True) or {}
111
- q = payload.get("question", "")
112
- lang = payload.get("selectedLanguage", "").strip()
113
- mode = payload.get("selectedMode", "").strip()
114
- prompt=""
115
- q2lang=""
116
- if not q:
117
- q = "Say hello in one short sentence."
118
- prompt="Image"+"%"+q+"%"+mode+"%"+lang
119
- print(prompt)
120
-
 
 
 
 
121
 
122
- return Response(
123
- stream_with_context(stream_openrouter(prompt)),
124
- mimetype="text/event-stream",
125
- headers={
126
- "Cache-Control": "no-cache",
127
- "X-Accel-Buffering": "no"
128
- }
129
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  if __name__ == "__main__":
132
  app.run(host="0.0.0.0", port=7860, debug=False)
 
1
  from flask import Flask, request, Response, jsonify, stream_with_context
2
  from flask_cors import CORS
3
+ import os, json, httpx, base64
4
  import config
5
 
 
6
  app = Flask(__name__)
7
  CORS(app)
8
 
9
  OPENROUTER_API_KEY = os.getenv("API_KEYS")
10
 
11
+ # File upload configuration
12
+ ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
13
+ MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB
14
+
15
+ def allowed_file(filename):
16
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
17
+
18
  @app.get("/health")
19
  def health():
20
  return jsonify({"status": "ready"}), 200
 
28
  }
29
 
30
  def _payload(q: str):
31
+ data = q.split("%")
32
+ content = ""
33
+ system = ""
34
+
35
+ if data[0] == "Text":
36
+ content = data[1]
37
  else:
38
+ content = {
39
+ "type": "image_url",
40
+ "image_url": {
41
+ "url": data[1]
42
+ }
43
  }
44
+
45
+ info = config.config
46
+ system = info[0][data[3]]
47
 
48
  return {
49
  "model": "meta-llama/llama-4-maverick-17b-128e-instruct",
50
  "stream": True,
51
+ "messages": [
52
+ {"role": "system", "content": system},
53
+ {"role": "user", "content": content}
54
+ ],
55
  "temperature": 0.7,
56
+ "top_p": 0.9,
57
  "max_tokens": 999,
58
  }
59
 
60
  def stream_openrouter(q: str):
61
  if not OPENROUTER_API_KEY:
62
+ yield "data: Error: Missing API key
63
+
64
+ "
65
  return
66
 
67
  try:
 
84
  content = json_data['choices'][0].get('delta', {}).get('content')
85
  print(content)
86
  if content:
87
+ yield f"data: {json.dumps(content)}
88
+
89
+ "
90
  except json.JSONDecodeError:
91
  continue
92
 
93
+ yield "data: [DONE]
94
+
95
+ "
96
 
97
  except httpx.HTTPError as e:
98
+ yield f"data: Error: {str(e)}
99
+
100
+ "
101
  except Exception as e:
102
+ yield f"data: Unexpected error: {str(e)}
103
+
104
+ "
105
 
106
  @app.post("/ask")
107
  def ask():
 
109
  q = payload.get("question", "").strip()
110
  lang = payload.get("selectedLanguage", "").strip()
111
  mode = payload.get("selectedMode", "").strip()
112
+
 
113
  if not q:
114
  q = "Say hello in one short sentence."
115
 
116
+ prompt = "Text" + "%" + q + "%" + mode + "%" + lang
117
  print(prompt)
118
 
119
  return Response(
 
127
 
128
  @app.post("/askimage")
129
  def askimage():
130
+ # Get form data instead of JSON
131
+ image_file = request.files.get('image')
132
+ lang = request.form.get('selectedLanguage', '').strip()
133
+ mode = request.form.get('selectedMode', '').strip()
134
+
135
+ if not image_file:
136
+ return jsonify({"error": "No image provided"}), 400
137
+
138
+ # Validate file type
139
+ if not allowed_file(image_file.filename):
140
+ return jsonify({"error": "Invalid file type. Allowed: png, jpg, jpeg, gif, webp"}), 400
141
+
142
+ try:
143
+ # Read image file
144
+ image_bytes = image_file.read()
145
 
146
+ # Check file size
147
+ if len(image_bytes) > MAX_FILE_SIZE:
148
+ return jsonify({"error": "File too large. Max size: 10MB"}), 400
149
+
150
+ # Convert to base64
151
+ base64_image = base64.b64encode(image_bytes).decode('utf-8')
152
+
153
+ # Determine image format
154
+ image_format = image_file.content_type
155
+ if not image_format:
156
+ # Fallback based on extension
157
+ ext = image_file.filename.rsplit('.', 1)[1].lower()
158
+ image_format = f"image/{ext}"
159
+
160
+ # Create data URL
161
+ image_data_url = f"data:{image_format};base64,{base64_image}"
162
+
163
+ # Build prompt in existing format
164
+ prompt = "Image" + "%" + image_data_url + "%" + mode + "%" + lang
165
+ print("Image upload received:", image_file.filename)
166
+
167
+ return Response(
168
+ stream_with_context(stream_openrouter(prompt)),
169
+ mimetype="text/event-stream",
170
+ headers={
171
+ "Cache-Control": "no-cache",
172
+ "X-Accel-Buffering": "no"
173
+ }
174
+ )
175
+ except Exception as e:
176
+ print(f"Error processing image: {str(e)}")
177
+ return jsonify({"error": str(e)}), 500
178
 
179
  if __name__ == "__main__":
180
  app.run(host="0.0.0.0", port=7860, debug=False)