Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
|
| 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 |
-
if
|
| 65 |
-
return
|
| 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 |
-
return
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|