-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
76 lines (62 loc) · 2.09 KB
/
app.py
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
import os
from flask import Flask, request, Response, g, render_template, jsonify
import marko
import google.generativeai as genai
import requests
# Configure generativeai
api_key= "YOUR_API_KEY"
genai.configure(api_key=api_key)
app = Flask(__name__)
app.debug = True
# Model configuration
config = {
'temperature': 0,
'top_k': 20,
'top_p': 0.9,
'max_output_tokens': 500
}
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"
}
]
model = genai.GenerativeModel(model_name="gemini-pro-vision",generation_config=config,safety_settings=safety_settings)
@app.route('/', methods=['GET'])
def hello_world():
return render_template("chat.html")
@app.route('/chat', methods=['POST'])
def chat():
# Get image URL from request
image_url = request.json.get('image_url')
# Fetch image data
image_response = requests.get(image_url)
if image_response.status_code != 200:
return jsonify({"error": "Failed to fetch image"})
image_data = image_response.content
prompt_parts = [
"You are Zuhair, an image descriptor. You will be given an image which will be of a maths question. You need to describe the image such that a person not seeing the image but your image description can exactly get what the image means. Donot miss any detail and donot give wrong description if not sure as it might not help in problem solving. \n\nUser's image:\n\n",
{
"mime_type": image_response.headers.get('content-type'),
"data": image_data
},
]
# Generate response
response = model.generate_content(prompt_parts)
return jsonify({
"response": marko.convert(response.text)
})
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))