Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,17 @@
|
|
| 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 |
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,40 +25,33 @@ def _headers():
|
|
| 28 |
}
|
| 29 |
|
| 30 |
def _payload(q: str):
|
| 31 |
-
data
|
| 32 |
-
content
|
| 33 |
-
system
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
content = data[1]
|
| 37 |
else:
|
| 38 |
-
content
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
}
|
| 44 |
-
|
| 45 |
-
info
|
| 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":
|
| 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,24 +74,16 @@ def stream_openrouter(q: str):
|
|
| 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,11 +91,12 @@ 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
|
| 117 |
print(prompt)
|
| 118 |
|
| 119 |
return Response(
|
|
@@ -135,34 +118,22 @@ def askimage():
|
|
| 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
|
| 156 |
-
|
| 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:{
|
| 162 |
|
| 163 |
-
# Build prompt in existing format
|
| 164 |
prompt = "Image" + "%" + image_data_url + "%" + mode + "%" + lang
|
| 165 |
-
print(
|
| 166 |
|
| 167 |
return Response(
|
| 168 |
stream_with_context(stream_openrouter(prompt)),
|
|
@@ -173,7 +144,6 @@ def askimage():
|
|
| 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__":
|
|
|
|
| 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 |
+
import base64
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
CORS(app)
|
| 12 |
|
| 13 |
OPENROUTER_API_KEY = os.getenv("API_KEYS")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
@app.get("/health")
|
| 16 |
def health():
|
| 17 |
return jsonify({"status": "ready"}), 200
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
def _payload(q: str):
|
| 28 |
+
data=q.split("%")
|
| 29 |
+
content=""
|
| 30 |
+
system=""
|
| 31 |
+
if data[0]=="Text":
|
| 32 |
+
content=data[1]
|
|
|
|
| 33 |
else:
|
| 34 |
+
content={
|
| 35 |
+
"type": "image_url",
|
| 36 |
+
"image_url": {
|
| 37 |
+
"url": data[1]
|
| 38 |
+
}
|
| 39 |
}
|
| 40 |
+
info=config.config
|
| 41 |
+
system=info[0][data[3]]
|
|
|
|
| 42 |
|
| 43 |
return {
|
| 44 |
"model": "meta-llama/llama-4-maverick-17b-128e-instruct",
|
| 45 |
"stream": True,
|
| 46 |
+
"messages": [{"role":"system","content":system},{"role": "user", "content": content}],
|
|
|
|
|
|
|
|
|
|
| 47 |
"temperature": 0.7,
|
| 48 |
+
"top_p":0.9,
|
| 49 |
"max_tokens": 999,
|
| 50 |
}
|
| 51 |
|
| 52 |
def stream_openrouter(q: str):
|
| 53 |
if not OPENROUTER_API_KEY:
|
| 54 |
+
yield "data: Error: Missing API key\n\n"
|
|
|
|
|
|
|
| 55 |
return
|
| 56 |
|
| 57 |
try:
|
|
|
|
| 74 |
content = json_data['choices'][0].get('delta', {}).get('content')
|
| 75 |
print(content)
|
| 76 |
if content:
|
| 77 |
+
yield f"data: {json.dumps(content)}\n\n"
|
|
|
|
|
|
|
| 78 |
except json.JSONDecodeError:
|
| 79 |
continue
|
| 80 |
|
| 81 |
+
yield "data: [DONE]\n\n"
|
|
|
|
|
|
|
| 82 |
|
| 83 |
except httpx.HTTPError as e:
|
| 84 |
+
yield f"data: Error: {str(e)}\n\n"
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
+
yield f"data: Unexpected error: {str(e)}\n\n"
|
|
|
|
|
|
|
| 87 |
|
| 88 |
@app.post("/ask")
|
| 89 |
def ask():
|
|
|
|
| 91 |
q = payload.get("question", "").strip()
|
| 92 |
lang = payload.get("selectedLanguage", "").strip()
|
| 93 |
mode = payload.get("selectedMode", "").strip()
|
| 94 |
+
prompt=""
|
| 95 |
+
q2lang=""
|
| 96 |
if not q:
|
| 97 |
q = "Say hello in one short sentence."
|
| 98 |
|
| 99 |
+
prompt="Text"+"%"+q+"%"+mode+"%"+lang
|
| 100 |
print(prompt)
|
| 101 |
|
| 102 |
return Response(
|
|
|
|
| 118 |
if not image_file:
|
| 119 |
return jsonify({"error": "No image provided"}), 400
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
try:
|
| 122 |
+
# Read image file and convert to base64
|
| 123 |
image_bytes = image_file.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
base64_image = base64.b64encode(image_bytes).decode('utf-8')
|
| 125 |
|
| 126 |
# Determine image format
|
| 127 |
+
image_format = image_file.content_type.split('/')[-1]
|
| 128 |
+
if image_format == 'jpeg':
|
| 129 |
+
image_format = 'jpg'
|
|
|
|
|
|
|
| 130 |
|
| 131 |
# Create data URL
|
| 132 |
+
image_data_url = f"data:{image_file.content_type};base64,{base64_image}"
|
| 133 |
|
| 134 |
+
# Build prompt in your existing format
|
| 135 |
prompt = "Image" + "%" + image_data_url + "%" + mode + "%" + lang
|
| 136 |
+
print(prompt)
|
| 137 |
|
| 138 |
return Response(
|
| 139 |
stream_with_context(stream_openrouter(prompt)),
|
|
|
|
| 144 |
}
|
| 145 |
)
|
| 146 |
except Exception as e:
|
|
|
|
| 147 |
return jsonify({"error": str(e)}), 500
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|