-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
92 lines (78 loc) · 3.47 KB
/
app.py
File metadata and controls
92 lines (78 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from flask import Flask, request, jsonify, send_from_directory
from werkzeug.utils import secure_filename
import requests
import os
import base64
from io import BytesIO
app = Flask(__name__)
# Configure upload folder and allowed extensions
UPLOAD_FOLDER = os.path.join(os.path.dirname(__file__), 'uploads')
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Retrieve OpenRouter API key and model name from environment variables
OPENROUTER_API_KEY = os.getenv('OPENROUTER_API_KEY')
OPENROUTER_MODEL = os.getenv('OPENROUTER_MODEL')
# Serve the index.html file
@app.route('/')
def serve_index():
return send_from_directory(os.path.dirname(__file__), 'index.html')
@app.route('/tools.json')
def serve_tools():
return send_from_directory(os.path.dirname(__file__), 'tools.json')
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# Handle API requests to /process
@app.route('/process', methods=['POST'])
def process_request():
try:
# Extract data from the incoming request
data = request.form
prompt = data.get('prompt')
print(f"Received prompt: {prompt}")
structured = data.get('structured', False)
if not prompt and not 'image' in request.files:
return jsonify({'error': 'Prompt is required if no image is provided'}), 400
# Send the request to OpenRouter
openrouter_url = 'https://openrouter.ai/api/v1/chat/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': f"Bearer {OPENROUTER_API_KEY}" # Use stored API key
}
payload = {
'model': OPENROUTER_MODEL, # Use stored model name
'messages': [
{
'role': 'user',
'content': prompt
}
]
}
# Check if the post request has the file part
if 'image' in request.files:
image = request.files['image']
if image and allowed_file(image.filename):
# Read the image file and encode it as a base64 data URL
image_data = image.read()
image_base64 = base64.b64encode(image_data).decode('utf-8')
# with open('b64.txt', 'w') as f:
# f.write(image_base64)
image_data_url = f"data:image/jpeg;base64,{image_base64}"
payload['messages'] = [
{"role": "user", "content": [
{"type": "text", "text": prompt.replace('text', 'image')},
{"type": "image_url", "image_url": {"url": image_data_url}}
]}
]
# print(f"Image data URL: {image_data_url[:100]}...")
print(f"Payload sent to OpenRouter: {str(payload)[0:500]}")
response = requests.post(openrouter_url, json=payload, headers=headers)
print(f"Response from OpenRouter: {response.status_code} - {response.text}")
if response.status_code != 200:
return jsonify({'error': 'Failed to process request with OpenRouter'}), response.status_code
# Return the response from OpenRouter
return jsonify(response.json()), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000) # Enable debug mode for development