Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
import PIL.Image
|
3 |
+
import io
|
4 |
+
import google.generativeai as genai
|
5 |
+
from werkzeug.utils import secure_filename
|
6 |
+
import os
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
app = Flask(__name__)
|
12 |
+
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
|
13 |
+
app.config['UPLOAD_FOLDER'] = 'uploads'
|
14 |
+
|
15 |
+
# Ensure upload folder exists
|
16 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
17 |
+
|
18 |
+
# Configure Google Generative AI
|
19 |
+
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
20 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
21 |
+
|
22 |
+
safety_settings = [
|
23 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
24 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
25 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
26 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
27 |
+
]
|
28 |
+
|
29 |
+
PROMPT_TEXT = """[Your existing prompt_text here]"""
|
30 |
+
PROMPT_IMAGE = """[Your existing prompt_image here]"""
|
31 |
+
|
32 |
+
@app.route('/')
|
33 |
+
def index():
|
34 |
+
return render_template('index.html')
|
35 |
+
|
36 |
+
@app.route('/analyze', methods=['POST'])
|
37 |
+
def analyze():
|
38 |
+
try:
|
39 |
+
analysis_type = request.form.get('analysis_type')
|
40 |
+
files = request.files.getlist('images')
|
41 |
+
|
42 |
+
if not files:
|
43 |
+
return jsonify({'error': 'No files uploaded'}), 400
|
44 |
+
|
45 |
+
image_parts = []
|
46 |
+
for file in files:
|
47 |
+
if file.filename:
|
48 |
+
# Save file temporarily
|
49 |
+
filename = secure_filename(file.filename)
|
50 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
51 |
+
file.save(filepath)
|
52 |
+
|
53 |
+
# Process image
|
54 |
+
image = PIL.Image.open(filepath)
|
55 |
+
image_parts.append(image)
|
56 |
+
|
57 |
+
# Clean up
|
58 |
+
os.remove(filepath)
|
59 |
+
|
60 |
+
# Select prompt based on analysis type
|
61 |
+
prompt = PROMPT_TEXT if analysis_type == 'text' else PROMPT_IMAGE
|
62 |
+
|
63 |
+
# Generate content
|
64 |
+
model = genai.GenerativeModel(model_name="gemini-2.0-flash-exp", safety_settings=safety_settings)
|
65 |
+
response = model.generate_content([prompt] + image_parts)
|
66 |
+
response.resolve()
|
67 |
+
|
68 |
+
return jsonify({'result': response.text})
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
return jsonify({'error': str(e)}), 500
|
72 |
+
|
73 |
+
if __name__ == '__main__':
|
74 |
+
app.run(debug=True)
|