Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,34 @@
|
|
| 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 |
import os
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
CHAT_API_KEY = os.getenv("CHAT_APIKEY")
|
| 10 |
-
IMAGINE_API_KEY = os.getenv("IMAGE_API_KEY")
|
| 11 |
-
secret_text = os.getenv("secret_text")
|
| 12 |
-
#Connect to index.html
|
| 13 |
app = Flask(__name__)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@app.route("/write", methods=["GET", "POST"])
|
| 17 |
def write():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if request.method == "GET":
|
| 19 |
return render_template("write.html")
|
| 20 |
|
|
@@ -24,8 +38,6 @@ def write():
|
|
| 24 |
types = request.form.get("type", "").strip()
|
| 25 |
word_limit = request.form.get("word_limit", "").strip()
|
| 26 |
|
| 27 |
-
print(f"\nRAW FORM DATA -> question: '{question}', type: '{types}', word_limit: '{word_limit}'\n-------------------------------\n")
|
| 28 |
-
|
| 29 |
if not question:
|
| 30 |
return jsonify({"error": "Please provide a question."}), 400
|
| 31 |
|
|
@@ -37,27 +49,44 @@ def write():
|
|
| 37 |
else:
|
| 38 |
word_limit = None
|
| 39 |
|
| 40 |
-
generativeai.configure(api_key=CHAT_API_KEY)
|
| 41 |
-
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
print(f"Error: {e}")
|
| 57 |
return jsonify({"error": "An error occurred while processing your request."}), 500
|
| 58 |
|
|
|
|
|
|
|
| 59 |
@app.route("/summarize", methods=["GET", "POST"])
|
| 60 |
def summarize():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
if request.method == "GET":
|
| 62 |
return render_template("summarize.html")
|
| 63 |
if request.method == "POST":
|
|
@@ -67,55 +96,47 @@ def summarize():
|
|
| 67 |
if not question:
|
| 68 |
return jsonify({"error": "Please provide a question."}), 400
|
| 69 |
|
| 70 |
-
generativeai.configure(api_key=CHAT_API_KEY)
|
| 71 |
-
|
| 72 |
try:
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
return jsonify({"answer": answer})
|
| 86 |
except Exception as e:
|
| 87 |
print(f"Error: {e}")
|
| 88 |
return jsonify({"error": "An error occurred while processing your request."}), 500
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
@app.route("/think", methods=["GET", "POST"])
|
| 92 |
-
def think():
|
| 93 |
-
if request.method == "GET":
|
| 94 |
-
return render_template("think.html")
|
| 95 |
-
if request.method == "POST":
|
| 96 |
-
|
| 97 |
-
question = request.form.get("question", "").strip()
|
| 98 |
-
if not question:
|
| 99 |
-
return jsonify({"error": "Please provide a question."}), 400
|
| 100 |
-
|
| 101 |
-
generativeai.configure(api_key=CHAT_API_KEY)
|
| 102 |
-
|
| 103 |
-
try:
|
| 104 |
-
model = generativeai.GenerativeModel('gemini-2.0-flash-thinking-exp-01-21')
|
| 105 |
-
response = model.generate_content(f"{secret_text} {question}.")
|
| 106 |
-
answer = response.text
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
print(f"Question: {question}\n------------------------- \n {answer} \n -------------------------\n")
|
| 110 |
-
|
| 111 |
-
return jsonify({"answer": answer})
|
| 112 |
-
except Exception as e:
|
| 113 |
-
print(f"Error: {e}")
|
| 114 |
-
return jsonify({"error": "An error occurred while processing your request."}), 500
|
| 115 |
|
| 116 |
|
| 117 |
@app.route("/translate", methods=["GET", "POST"])
|
| 118 |
def translate():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
if request.method == "GET":
|
| 120 |
return render_template("translate.html")
|
| 121 |
if request.method == "POST":
|
|
@@ -124,80 +145,67 @@ def translate():
|
|
| 124 |
translate_to = request.form.get("translate_to", "").strip()
|
| 125 |
if not question:
|
| 126 |
return jsonify({"error": "Please provide a question."}), 400
|
| 127 |
-
|
| 128 |
-
generativeai.configure(api_key=CHAT_API_KEY)
|
| 129 |
-
|
| 130 |
try:
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
return jsonify({"answer": answer})
|
| 139 |
except Exception as e:
|
| 140 |
print(f"Error: {e}")
|
| 141 |
return jsonify({"error": "An error occurred while processing your request."}), 500
|
| 142 |
|
| 143 |
-
@app.route("/imagine", methods=["GET", "POST"])
|
| 144 |
-
def imagine():
|
| 145 |
-
if request.method == "GET":
|
| 146 |
-
return render_template("imagine.html")
|
| 147 |
-
if request.method == "POST":
|
| 148 |
-
contents = request.form.get("contents", "").strip()
|
| 149 |
-
if not contents:
|
| 150 |
-
return jsonify({"error": "Please provide a prompt."}), 400
|
| 151 |
-
|
| 152 |
-
client = genai.Client(api_key=IMAGINE_API_KEY)
|
| 153 |
-
|
| 154 |
-
response = client.models.generate_content(
|
| 155 |
-
model="gemini-2.0-flash-preview-image-generation",
|
| 156 |
-
contents=contents,
|
| 157 |
-
config=types.GenerateContentConfig(
|
| 158 |
-
response_modalities=['TEXT', 'IMAGE']
|
| 159 |
-
)
|
| 160 |
-
)
|
| 161 |
-
print(f"\nPROMPT: {contents}\n")
|
| 162 |
-
for part in response.candidates[0].content.parts:
|
| 163 |
-
if part.inline_data is not None:
|
| 164 |
-
image = Image.open(BytesIO(part.inline_data.data))
|
| 165 |
-
img_io = BytesIO()
|
| 166 |
-
image.save(img_io, format="PNG")
|
| 167 |
-
img_io.seek(0)
|
| 168 |
-
return send_file(img_io, mimetype="image/png")
|
| 169 |
-
|
| 170 |
-
return jsonify({"error": "No image returned by model"}), 500
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
@app.route("/")
|
| 174 |
-
def index():
|
| 175 |
-
return render_template("index.html")
|
| 176 |
|
| 177 |
@app.route("/ask", methods=["POST"])
|
| 178 |
def ask():
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
question = request.form.get("question", "").strip()
|
| 181 |
if not question:
|
| 182 |
return jsonify({"error": "Please provide a question."}), 400
|
| 183 |
|
| 184 |
-
generativeai.configure(api_key=CHAT_API_KEY)
|
| 185 |
-
|
| 186 |
try:
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
except Exception as e:
|
| 197 |
print(f"Error: {e}")
|
| 198 |
return jsonify({"error": "An error occurred while processing your request."}), 500
|
| 199 |
|
| 200 |
if __name__ == '__main__':
|
| 201 |
|
| 202 |
-
app.run(host="0.0.0.0", port=7860)
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
from flask import Flask, request, jsonify, render_template, send_file
|
| 4 |
+
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
client = OpenAI(
|
| 9 |
+
base_url="https://router.huggingface.co/v1",
|
| 10 |
+
api_key=os.environ["HF_TOKEN"],
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
app = Flask(__name__)
|
| 14 |
+
@app.route("/")
|
| 15 |
+
def index():
|
| 16 |
+
return render_template("index.html")
|
| 17 |
|
| 18 |
@app.route("/write", methods=["GET", "POST"])
|
| 19 |
def write():
|
| 20 |
+
|
| 21 |
+
WRITE_PROMPT = """
|
| 22 |
+
You are TaskBot AI created by Advay Singh.
|
| 23 |
+
|
| 24 |
+
Task:
|
| 25 |
+
Write high-quality content based on the user's request.
|
| 26 |
+
|
| 27 |
+
Rules:
|
| 28 |
+
- Output only the requested content.
|
| 29 |
+
- Do not say "Here is your essay/speech".
|
| 30 |
+
- Do not chat with the user.
|
| 31 |
+
"""
|
| 32 |
if request.method == "GET":
|
| 33 |
return render_template("write.html")
|
| 34 |
|
|
|
|
| 38 |
types = request.form.get("type", "").strip()
|
| 39 |
word_limit = request.form.get("word_limit", "").strip()
|
| 40 |
|
|
|
|
|
|
|
| 41 |
if not question:
|
| 42 |
return jsonify({"error": "Please provide a question."}), 400
|
| 43 |
|
|
|
|
| 49 |
else:
|
| 50 |
word_limit = None
|
| 51 |
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
+
completion = client.chat.completions.create(
|
| 54 |
+
model="Qwen/Qwen2.5-7B-Instruct:together",
|
| 55 |
+
messages=[
|
| 56 |
+
{
|
| 57 |
+
"role": "system",
|
| 58 |
+
"content": WRITE_PROMPT
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"role": "user",
|
| 62 |
+
"content": f"Write a {types if types else 'paragraph'} on the topic '{question} in about {word_limit} words.'"
|
| 63 |
+
}
|
| 64 |
+
],
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
response = completion.choices[0].message.content
|
| 68 |
+
print(f"WRITE ABOUT: \n{question} in {types} in minimum {word_limit}\n------------------------- \n AI RESPONSE: {response} \n--------------------------")
|
| 69 |
+
return jsonify({"answer": response})
|
| 70 |
+
|
| 71 |
except Exception as e:
|
| 72 |
print(f"Error: {e}")
|
| 73 |
return jsonify({"error": "An error occurred while processing your request."}), 500
|
| 74 |
|
| 75 |
+
|
| 76 |
+
|
| 77 |
@app.route("/summarize", methods=["GET", "POST"])
|
| 78 |
def summarize():
|
| 79 |
+
SUMMARIZE_PROMPT = """
|
| 80 |
+
You are TaskBot AI created by Advay Singh.
|
| 81 |
+
|
| 82 |
+
Task:
|
| 83 |
+
Summarize the given text.
|
| 84 |
+
|
| 85 |
+
Rules:
|
| 86 |
+
- Output ONLY the summary.
|
| 87 |
+
- Do not add introductions like "Here is the summary".
|
| 88 |
+
- Do not chat with the user.
|
| 89 |
+
"""
|
| 90 |
if request.method == "GET":
|
| 91 |
return render_template("summarize.html")
|
| 92 |
if request.method == "POST":
|
|
|
|
| 96 |
if not question:
|
| 97 |
return jsonify({"error": "Please provide a question."}), 400
|
| 98 |
|
|
|
|
|
|
|
| 99 |
try:
|
| 100 |
|
| 101 |
+
completion = client.chat.completions.create(
|
| 102 |
+
model="Qwen/Qwen2.5-7B-Instruct:together",
|
| 103 |
+
messages=[
|
| 104 |
+
{
|
| 105 |
+
"role": "system",
|
| 106 |
+
"content": SUMMARIZE_PROMPT
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"role": "user",
|
| 110 |
+
"content": f"Summarize: \n '{question}' nearly about {minimum_lines_points if minimum_lines_points else 5} {types}."
|
| 111 |
+
}
|
| 112 |
+
],
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
|
| 116 |
+
response = completion.choices[0].message.content
|
| 117 |
+
|
| 118 |
+
print(f"SUMMARIZE: \n{question} in {types} in minimum {minimum_lines_points} {types}\n------------------------- \n AI RESPONSE: {response} \n--------------------------")
|
| 119 |
+
|
| 120 |
+
return jsonify({"answer": response})
|
| 121 |
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
print(f"Error: {e}")
|
| 124 |
return jsonify({"error": "An error occurred while processing your request."}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
@app.route("/translate", methods=["GET", "POST"])
|
| 128 |
def translate():
|
| 129 |
+
TRANSLATE_PROMPT = """
|
| 130 |
+
You are TaskBot AI created by Advay Singh.
|
| 131 |
+
|
| 132 |
+
Task:
|
| 133 |
+
Translate the given text to the requested language.
|
| 134 |
+
|
| 135 |
+
Rules:
|
| 136 |
+
- Output ONLY the translated text.
|
| 137 |
+
- Do not add explanations.
|
| 138 |
+
- Do not say "Here is the translation" or something else like that, just give the answer directly.
|
| 139 |
+
"""
|
| 140 |
if request.method == "GET":
|
| 141 |
return render_template("translate.html")
|
| 142 |
if request.method == "POST":
|
|
|
|
| 145 |
translate_to = request.form.get("translate_to", "").strip()
|
| 146 |
if not question:
|
| 147 |
return jsonify({"error": "Please provide a question."}), 400
|
| 148 |
+
|
|
|
|
|
|
|
| 149 |
try:
|
| 150 |
|
| 151 |
+
completion = client.chat.completions.create(
|
| 152 |
+
model="Qwen/Qwen2.5-7B-Instruct:together",
|
| 153 |
+
messages=[
|
| 154 |
+
{
|
| 155 |
+
"role": "system",
|
| 156 |
+
"content": TRANSLATE_PROMPT
|
| 157 |
+
},
|
| 158 |
+
{
|
| 159 |
+
"role": "user",
|
| 160 |
+
"content": f"translate {question} from {translate_from} to {translate_to}."
|
| 161 |
+
}
|
| 162 |
+
],
|
| 163 |
+
)
|
| 164 |
|
| 165 |
+
response = completion.choices[0].message.content
|
| 166 |
+
print(f"Translate: {question} from {translate_from} to {translate_to}\n------------------------- \n AI RESPONSE: {response} \n--------------------------")
|
| 167 |
+
|
| 168 |
+
return jsonify({"answer": response})
|
| 169 |
|
|
|
|
| 170 |
except Exception as e:
|
| 171 |
print(f"Error: {e}")
|
| 172 |
return jsonify({"error": "An error occurred while processing your request."}), 500
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
@app.route("/ask", methods=["POST"])
|
| 176 |
def ask():
|
| 177 |
+
CHAT_PROMPT = """
|
| 178 |
+
You are TaskBot AI created by Advay Singh.
|
| 179 |
+
|
| 180 |
+
Respond normally and conversationally.
|
| 181 |
+
Never mention the underlying AI model.
|
| 182 |
+
"""
|
| 183 |
question = request.form.get("question", "").strip()
|
| 184 |
if not question:
|
| 185 |
return jsonify({"error": "Please provide a question."}), 400
|
| 186 |
|
|
|
|
|
|
|
| 187 |
try:
|
| 188 |
+
completion = client.chat.completions.create(
|
| 189 |
+
model="Qwen/Qwen2.5-7B-Instruct:together",
|
| 190 |
+
messages=[
|
| 191 |
+
{
|
| 192 |
+
"role": "system",
|
| 193 |
+
"content": CHAT_PROMPT
|
| 194 |
+
},
|
| 195 |
+
{
|
| 196 |
+
"role": "user",
|
| 197 |
+
"content": f"{question}"
|
| 198 |
+
}
|
| 199 |
+
],
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
response = completion.choices[0].message.content
|
| 203 |
+
print(f"QUESTION: \n{question} \n-------------------------------- AI RESPONSE: \n{response} \n --------------------------------")
|
| 204 |
+
return jsonify({"answer": response})
|
| 205 |
except Exception as e:
|
| 206 |
print(f"Error: {e}")
|
| 207 |
return jsonify({"error": "An error occurred while processing your request."}), 500
|
| 208 |
|
| 209 |
if __name__ == '__main__':
|
| 210 |
|
| 211 |
+
app.run(host="0.0.0.0", port=7860)
|
|
|