-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (65 loc) · 2.06 KB
/
app.py
File metadata and controls
79 lines (65 loc) · 2.06 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
from flask import Flask, render_template, request, redirect, url_for
import google.generativeai as genai
import os
import time
from PIL import Image
from io import BytesIO
app = Flask(__name__)
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
}
]
config = {
'temperature': 0,
'top_k': 20,
'top_p': 0.9,
'max_output_tokens': 500
}
# gemini-pro-vision
model_vision = genai.GenerativeModel(model_name="gemini-pro-vision",
generation_config=config,
safety_settings=safety_settings)
# gemini-pro model
model_gemini = genai.GenerativeModel('gemini-pro')
genai.configure(api_key=os.getenv('api_key'))
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
try:
prompt = request.form['prompt']
image = request.files['image'] if 'image' in request.files else None
# Use gemini-pro-vision if an image is uploaded
model = model_vision if image else model_gemini
# Handle both text and image prompts
question = [prompt, process_image(image)] if image else prompt
response = model.generate_content(question, stream=True)
response.resolve()
if response.text:
return response.text
else:
return "Sorry, but I didn't want to answer that!"
except Exception as e:
return "Sorry, but WaziAI didn't want to answer that!"
return render_template('index.html', **locals())
def process_image(image):
if image:
# Process the image using PIL
img = Image.open(image)
return img
return None
if __name__ == '__main__':
app.run(debug=True)