Advay-Singh commited on
Commit
1f76cca
·
verified ·
1 Parent(s): 96063ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +205 -199
app.py CHANGED
@@ -1,200 +1,206 @@
1
- import google.generativeai as generativeai
2
- from flask import Flask, request, jsonify, render_template, send_file
3
- from google.genai import types
4
- from PIL import Image
5
- from io import BytesIO
6
- from google import genai
7
-
8
-
9
- #Connect to index.html
10
- app = Flask(__name__)
11
-
12
-
13
- @app.route("/write", methods=["GET", "POST"])
14
- def write():
15
- if request.method == "GET":
16
- return render_template("write.html")
17
-
18
- if request.method == "POST":
19
- # Getting data from form
20
- question = request.form.get("question", "").strip()
21
- types = request.form.get("type", "").strip()
22
- word_limit = request.form.get("word_limit", "").strip()
23
-
24
- print(f"\nRAW FORM DATA -> question: '{question}', type: '{types}', word_limit: '{word_limit}'\n-------------------------------\n")
25
-
26
- if not question:
27
- return jsonify({"error": "Please provide a question."}), 400
28
-
29
- if word_limit:
30
- try:
31
- word_limit = float(word_limit)
32
- except ValueError:
33
- return jsonify({"error": "Word limit must be a number."}), 400
34
- else:
35
- word_limit = None
36
-
37
- generativeai.configure(api_key="AIzaSyAa4WXqagEm5XgWe2jkpkb7ggYL2HiB8LU")
38
-
39
- try:
40
- model = generativeai.GenerativeModel("gemini-2.0-flash")
41
- prompt = (
42
- f"You are TaskBot AI created by Advay Singh and powered by Gemini AI. "
43
- f"Write a {types if types else 'paragraph'} on the topic '{question}'"
44
- )
45
- if word_limit:
46
- prompt += f" nearly about {word_limit} words."
47
-
48
- response = model.generate_content(prompt)
49
- print(f"ANSWER BY TASKBOT AI: \n {response.text}")
50
- return jsonify({"answer": response.text})
51
-
52
- except Exception as e:
53
- print(f"Error: {e}")
54
- return jsonify({"error": "An error occurred while processing your request."}), 500
55
-
56
- @app.route("/summarize", methods=["GET", "POST"])
57
- def summarize():
58
- if request.method == "GET":
59
- return render_template("summarize.html")
60
- if request.method == "POST":
61
- question = request.form.get("question", "").strip()
62
- types = request.form.get("type")
63
- minimum_lines_points = request.form.get("num_of_lines_points")
64
- if not question:
65
- return jsonify({"error": "Please provide a question."}), 400
66
-
67
- generativeai.configure(api_key="AIzaSyAa4WXqagEm5XgWe2jkpkb7ggYL2HiB8LU")
68
-
69
- try:
70
-
71
- model = generativeai.GenerativeModel('gemini-2.0-flash')
72
- prompt = (
73
- f"You are TaskBot AI created by Advay Singh and powered by Gemini AI. "
74
- f"Write a {types if types else 'paragraph'} on the topic '{question}'")
75
- if minimum_lines_points:
76
- prompt += f" nearly about {minimum_lines_points} {types}."
77
- response = model.generate_content(prompt)
78
- answer = response.text
79
-
80
- print(f"Raw Text: \n{question}; Type: {types}; Minimum {types}: {minimum_lines_points}\n------------------------- \n {answer} \n -------------------------")
81
-
82
- return jsonify({"answer": answer})
83
- except Exception as e:
84
- print(f"Error: {e}")
85
- return jsonify({"error": "An error occurred while processing your request."}), 500
86
-
87
-
88
- @app.route("/think", methods=["GET", "POST"])
89
- def think():
90
- if request.method == "GET":
91
- return render_template("think.html")
92
- if request.method == "POST":
93
-
94
- question = request.form.get("question", "").strip()
95
- if not question:
96
- return jsonify({"error": "Please provide a question."}), 400
97
-
98
- generativeai.configure(api_key="AIzaSyAa4WXqagEm5XgWe2jkpkb7ggYL2HiB8LU")
99
-
100
- try:
101
- model = generativeai.GenerativeModel('gemini-2.0-flash-thinking-exp-01-21')
102
- response = model.generate_content(f"You are TaskBot AI created by Advay Singh and powered by Gemini AI. Remember that and don't say anything (not even ok) about that just answer me this question- {question}.")
103
- answer = response.text
104
-
105
-
106
- print(f"Question: {question}\n------------------------- \n {answer} \n -------------------------\n")
107
-
108
- return jsonify({"answer": answer})
109
- except Exception as e:
110
- print(f"Error: {e}")
111
- return jsonify({"error": "An error occurred while processing your request."}), 500
112
-
113
-
114
- @app.route("/translate", methods=["GET", "POST"])
115
- def translate():
116
- if request.method == "GET":
117
- return render_template("translate.html")
118
- if request.method == "POST":
119
- question = request.form.get("question", "").strip()
120
- translate_from = request.form.get("translate_from", "").strip()
121
- translate_to = request.form.get("translate_to", "").strip()
122
- if not question:
123
- return jsonify({"error": "Please provide a question."}), 400
124
-
125
- generativeai.configure(api_key="AIzaSyAa4WXqagEm5XgWe2jkpkb7ggYL2HiB8LU")
126
-
127
- try:
128
-
129
- model = generativeai.GenerativeModel('gemini-2.0-flash')
130
- response = model.generate_content(f"You are TaskBot AI created by Advay Singh and powered by Gemini AI remember this and don't say anything about this unitll asked (not even ok). Just translate {question} from {translate_from} to {translate_to} and nothing else. ")
131
- answer = response.text
132
-
133
- print(f"Translate: {question} from {translate_from} to {translate_to}\n------------------------- \n {answer} \n--------------------------")
134
-
135
- return jsonify({"answer": answer})
136
- except Exception as e:
137
- print(f"Error: {e}")
138
- return jsonify({"error": "An error occurred while processing your request."}), 500
139
-
140
- @app.route("/imagine", methods=["GET", "POST"])
141
- def imagine():
142
- if request.method == "GET":
143
- return render_template("imagine.html")
144
- if request.method == "POST":
145
- contents = request.form.get("contents", "").strip()
146
- if not contents:
147
- return jsonify({"error": "Please provide a prompt."}), 400
148
-
149
- client = genai.Client(api_key="AIzaSyC29gcbTAWKK99f6V9T07bNLz0IefRBKWc")
150
-
151
- response = client.models.generate_content(
152
- model="gemini-2.0-flash-preview-image-generation",
153
- contents=contents,
154
- config=types.GenerateContentConfig(
155
- response_modalities=['TEXT', 'IMAGE']
156
- )
157
- )
158
- print(f"\nPROMPT: {contents}\n")
159
- for part in response.candidates[0].content.parts:
160
- if part.inline_data is not None:
161
- image = Image.open(BytesIO(part.inline_data.data))
162
- img_io = BytesIO()
163
- image.save(img_io, format="PNG")
164
- img_io.seek(0)
165
- return send_file(img_io, mimetype="image/png")
166
-
167
- return jsonify({"error": "No image returned by model"}), 500
168
-
169
-
170
- @app.route("/")
171
- def index():
172
- return render_template("index.html")
173
-
174
- @app.route("/ask", methods=["POST"])
175
- def ask():
176
- #getting the question from the form55
177
- question = request.form.get("question", "").strip()
178
- if not question:
179
- return jsonify({"error": "Please provide a question."}), 400
180
-
181
- generativeai.configure(api_key="AIzaSyAa4WXqagEm5XgWe2jkpkb7ggYL2HiB8LU")
182
-
183
- try:
184
- # use Google's Gemini-2.0-Flash nodle for generating content
185
- model = generativeai.GenerativeModel('gemini-2.0-flash')
186
- response = model.generate_content(f"You are TaskBot AI created by Advay Singh and powered by Gemini AI. Remember that and don't say anything (not even ok) about that just answer me this question- {question}.")
187
- answer = response.text
188
-
189
- # Log the question and answer for debugging
190
- print(f"Question: {question}\n------------------------- \n {answer} \n -------------------------")
191
- # Return the answer as JSON
192
- return jsonify({"answer": answer})
193
- except Exception as e:
194
- print(f"Error: {e}")
195
- return jsonify({"error": "An error occurred while processing your request."}), 500
196
-
197
- if __name__ == '__main__':
198
-
199
- app.run(host="0.0.0.0", port=7860)
 
 
 
 
 
 
200
 
 
1
+ import google.generativeai as generativeai
2
+ from flask import Flask, request, jsonify, render_template, send_file
3
+ from google.genai import types
4
+ from PIL import Image
5
+ from io import BytesIO
6
+ from google import genai
7
+
8
+ @app.before_request
9
+ def block_direct_access():
10
+ allowed_referrer = "https://taskbot-app.netlify.app"
11
+ if request.referrer and allowed_referrer not in request.referrer:
12
+ return "Access denied", 403
13
+
14
+
15
+ #Connect to index.html
16
+ app = Flask(__name__)
17
+
18
+
19
+ @app.route("/write", methods=["GET", "POST"])
20
+ def write():
21
+ if request.method == "GET":
22
+ return render_template("write.html")
23
+
24
+ if request.method == "POST":
25
+ # Getting data from form
26
+ question = request.form.get("question", "").strip()
27
+ types = request.form.get("type", "").strip()
28
+ word_limit = request.form.get("word_limit", "").strip()
29
+
30
+ print(f"\nRAW FORM DATA -> question: '{question}', type: '{types}', word_limit: '{word_limit}'\n-------------------------------\n")
31
+
32
+ if not question:
33
+ return jsonify({"error": "Please provide a question."}), 400
34
+
35
+ if word_limit:
36
+ try:
37
+ word_limit = float(word_limit)
38
+ except ValueError:
39
+ return jsonify({"error": "Word limit must be a number."}), 400
40
+ else:
41
+ word_limit = None
42
+
43
+ generativeai.configure(api_key="AIzaSyAa4WXqagEm5XgWe2jkpkb7ggYL2HiB8LU")
44
+
45
+ try:
46
+ model = generativeai.GenerativeModel("gemini-2.0-flash")
47
+ prompt = (
48
+ f"You are TaskBot AI created by Advay Singh and powered by Gemini AI. "
49
+ f"Write a {types if types else 'paragraph'} on the topic '{question}'"
50
+ )
51
+ if word_limit:
52
+ prompt += f" nearly about {word_limit} words."
53
+
54
+ response = model.generate_content(prompt)
55
+ print(f"ANSWER BY TASKBOT AI: \n {response.text}")
56
+ return jsonify({"answer": response.text})
57
+
58
+ except Exception as e:
59
+ print(f"Error: {e}")
60
+ return jsonify({"error": "An error occurred while processing your request."}), 500
61
+
62
+ @app.route("/summarize", methods=["GET", "POST"])
63
+ def summarize():
64
+ if request.method == "GET":
65
+ return render_template("summarize.html")
66
+ if request.method == "POST":
67
+ question = request.form.get("question", "").strip()
68
+ types = request.form.get("type")
69
+ minimum_lines_points = request.form.get("num_of_lines_points")
70
+ if not question:
71
+ return jsonify({"error": "Please provide a question."}), 400
72
+
73
+ generativeai.configure(api_key="AIzaSyAa4WXqagEm5XgWe2jkpkb7ggYL2HiB8LU")
74
+
75
+ try:
76
+
77
+ model = generativeai.GenerativeModel('gemini-2.0-flash')
78
+ prompt = (
79
+ f"You are TaskBot AI created by Advay Singh and powered by Gemini AI. "
80
+ f"Write a {types if types else 'paragraph'} on the topic '{question}'")
81
+ if minimum_lines_points:
82
+ prompt += f" nearly about {minimum_lines_points} {types}."
83
+ response = model.generate_content(prompt)
84
+ answer = response.text
85
+
86
+ print(f"Raw Text: \n{question}; Type: {types}; Minimum {types}: {minimum_lines_points}\n------------------------- \n {answer} \n -------------------------")
87
+
88
+ return jsonify({"answer": answer})
89
+ except Exception as e:
90
+ print(f"Error: {e}")
91
+ return jsonify({"error": "An error occurred while processing your request."}), 500
92
+
93
+
94
+ @app.route("/think", methods=["GET", "POST"])
95
+ def think():
96
+ if request.method == "GET":
97
+ return render_template("think.html")
98
+ if request.method == "POST":
99
+
100
+ question = request.form.get("question", "").strip()
101
+ if not question:
102
+ return jsonify({"error": "Please provide a question."}), 400
103
+
104
+ generativeai.configure(api_key="AIzaSyAa4WXqagEm5XgWe2jkpkb7ggYL2HiB8LU")
105
+
106
+ try:
107
+ model = generativeai.GenerativeModel('gemini-2.0-flash-thinking-exp-01-21')
108
+ response = model.generate_content(f"You are TaskBot AI created by Advay Singh and powered by Gemini AI. Remember that and don't say anything (not even ok) about that just answer me this question- {question}.")
109
+ answer = response.text
110
+
111
+
112
+ print(f"Question: {question}\n------------------------- \n {answer} \n -------------------------\n")
113
+
114
+ return jsonify({"answer": answer})
115
+ except Exception as e:
116
+ print(f"Error: {e}")
117
+ return jsonify({"error": "An error occurred while processing your request."}), 500
118
+
119
+
120
+ @app.route("/translate", methods=["GET", "POST"])
121
+ def translate():
122
+ if request.method == "GET":
123
+ return render_template("translate.html")
124
+ if request.method == "POST":
125
+ question = request.form.get("question", "").strip()
126
+ translate_from = request.form.get("translate_from", "").strip()
127
+ translate_to = request.form.get("translate_to", "").strip()
128
+ if not question:
129
+ return jsonify({"error": "Please provide a question."}), 400
130
+
131
+ generativeai.configure(api_key="AIzaSyAa4WXqagEm5XgWe2jkpkb7ggYL2HiB8LU")
132
+
133
+ try:
134
+
135
+ model = generativeai.GenerativeModel('gemini-2.0-flash')
136
+ response = model.generate_content(f"You are TaskBot AI created by Advay Singh and powered by Gemini AI remember this and don't say anything about this unitll asked (not even ok). Just translate {question} from {translate_from} to {translate_to} and nothing else. ")
137
+ answer = response.text
138
+
139
+ print(f"Translate: {question} from {translate_from} to {translate_to}\n------------------------- \n {answer} \n--------------------------")
140
+
141
+ return jsonify({"answer": answer})
142
+ except Exception as e:
143
+ print(f"Error: {e}")
144
+ return jsonify({"error": "An error occurred while processing your request."}), 500
145
+
146
+ @app.route("/imagine", methods=["GET", "POST"])
147
+ def imagine():
148
+ if request.method == "GET":
149
+ return render_template("imagine.html")
150
+ if request.method == "POST":
151
+ contents = request.form.get("contents", "").strip()
152
+ if not contents:
153
+ return jsonify({"error": "Please provide a prompt."}), 400
154
+
155
+ client = genai.Client(api_key="AIzaSyC29gcbTAWKK99f6V9T07bNLz0IefRBKWc")
156
+
157
+ response = client.models.generate_content(
158
+ model="gemini-2.0-flash-preview-image-generation",
159
+ contents=contents,
160
+ config=types.GenerateContentConfig(
161
+ response_modalities=['TEXT', 'IMAGE']
162
+ )
163
+ )
164
+ print(f"\nPROMPT: {contents}\n")
165
+ for part in response.candidates[0].content.parts:
166
+ if part.inline_data is not None:
167
+ image = Image.open(BytesIO(part.inline_data.data))
168
+ img_io = BytesIO()
169
+ image.save(img_io, format="PNG")
170
+ img_io.seek(0)
171
+ return send_file(img_io, mimetype="image/png")
172
+
173
+ return jsonify({"error": "No image returned by model"}), 500
174
+
175
+
176
+ @app.route("/")
177
+ def index():
178
+ return render_template("index.html")
179
+
180
+ @app.route("/ask", methods=["POST"])
181
+ def ask():
182
+ #getting the question from the form55
183
+ question = request.form.get("question", "").strip()
184
+ if not question:
185
+ return jsonify({"error": "Please provide a question."}), 400
186
+
187
+ generativeai.configure(api_key="AIzaSyAa4WXqagEm5XgWe2jkpkb7ggYL2HiB8LU")
188
+
189
+ try:
190
+ # use Google's Gemini-2.0-Flash nodle for generating content
191
+ model = generativeai.GenerativeModel('gemini-2.0-flash')
192
+ response = model.generate_content(f"You are TaskBot AI created by Advay Singh and powered by Gemini AI. Remember that and don't say anything (not even ok) about that just answer me this question- {question}.")
193
+ answer = response.text
194
+
195
+ # Log the question and answer for debugging
196
+ print(f"Question: {question}\n------------------------- \n {answer} \n -------------------------")
197
+ # Return the answer as JSON
198
+ return jsonify({"answer": answer})
199
+ except Exception as e:
200
+ print(f"Error: {e}")
201
+ return jsonify({"error": "An error occurred while processing your request."}), 500
202
+
203
+ if __name__ == '__main__':
204
+
205
+ app.run(host="0.0.0.0", port=7860)
206