-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
387 lines (298 loc) · 10.9 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
from flask import Flask, render_template, request, jsonify
import base64
import os
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from dotenv import load_dotenv
from bson.objectid import ObjectId
from datetime import datetime
# Custom modules
import google_handlers
import gemini
import pollinations
import pdf_handler
load_dotenv()
app = Flask(__name__)
# * MongoDB Setup
mongo_db = None
mongo_collection = None
mongodb_uri = os.getenv("MONGODB_URI")
client = MongoClient(mongodb_uri, server_api=ServerApi('1'))
mongo_db = client['PolyPersona_DB']
mongo_collection = mongo_db['generated_responses']
# Send a ping to confirm a successful connection to MongoDB
TEST_CONNECTION = True
if TEST_CONNECTION:
print("=== Testing MongoDB Connection ===")
try:
client.admin.command('ping')
print("✅ Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
# Add a sample document in the collection (don't add if already exists)
name = 'John Doe'
if mongo_collection.count_documents({'name': name}) == 0:
mongo_collection.insert_one({'name': name})
print("✅ Added a sample document in the collection.")
# Retrieve the sample document added
result = mongo_collection.find_one({'name': name})
print(f"✅ Retrieved the sample document: {result}")
# Delete the sample document added
mongo_collection.delete_one({'name': name})
print("✅ Deleted the sample document.")
print("=== MongoDB Connection Test Completed ===")
# ? Constants
# Sample Celebrities
SAMPLE_CELEBS = {
"Marcus Aurelius": {
"say": "Thoughts on Social Media & Gen-Z",
"image_src": "./static/images/cards/marcus_aurelius.png"
},
"Buddha": {
"say": "Entrepreneurship & Correlation with Mental Health",
"image_src": "./static/images/cards/buddha.jpg"
},
"Albert Einstein": {
"say": "Existentialism in the Digital Age",
"image_src": "./static/images/cards/einstein.jpg"
},
"Chanakya": {
"say": "48 Laws of Power & his interpretation of it",
"image_src": "./static/images/cards/chanakya.jpg"
},
"Goku": {
"say": "The importance of training & discipline",
"image_src": "./static/images/cards/goku.jpg"
},
}
# Fine tune options
FINE_TUNE_OPTIONS = [
"Funny",
"More detailed",
"More concise",
"More casual",
"More formal",
"More professional",
"More emotional",
"Engaging",
"Deep",
"Light-hearted",
]
# ? Helper functions
def get_audio_base64(audio_path):
"""Converts the audio file to base64.
Args
----
- `audio_path`: Path to the audio file.
Returns
-------
- `audio_base64`: Base64 encoded audio.
"""
with open(audio_path, "rb") as audio_file:
audio_bytes = audio_file.read()
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
return audio_base64
def recent_responses():
"""
Fetch the 4 most recent responses from MongoDB.
Returns:
- List of 4 most recent response documents
"""
recent_docs = list(mongo_collection.find().sort("created_at", -1).limit(4))
# Convert ObjectId to string for JSON serialization
for doc in recent_docs:
doc['_id'] = str(doc['_id'])
return recent_docs
def render_index(**kwargs):
"""Renders the index page with the given keyword arguments.
Args
----
- `**kwargs`: Keyword arguments to pass to the template. Eg. `error`, `result`, etc.
Returns
-------
- `render_template`: Renders the index page with the given keyword arguments.
"""
return render_template("index.html",
LANG_MAP=google_handlers.LANG_MAP,
SAMPLE_CELEBS=SAMPLE_CELEBS,
FINE_TUNE_OPTIONS=FINE_TUNE_OPTIONS,
recent_responses=recent_responses(),
**kwargs)
def save_response_to_mongodb(result):
"""
Save the generated response to MongoDB.
Parameters:
- result (dict): The dictionary containing response details
Returns:
- ObjectId of the inserted document
"""
# Prepare the document to save
document = {
"celebrity": result["celeb"],
"topic": result["say_what"],
"language": result["target_language"],
"response": result["response"],
"model_parameters": {
"temperature": result["temperature"],
"top_k": result["top_k"],
"top_p": result["top_p"]
},
"image_path": result["image_path"],
"created_at": datetime.now()
}
# Insert the document and return its ID
return mongo_collection.insert_one(document).inserted_id
# ? Routes
# / : Home page
# /celeb : To get the response for a celebrity
# /translate_api : To translate a text
# /fine_tune_api : To fine-tune a text
# /generate_pdf : To generate a PDF with the response with embedded QR code to the website
@app.route('/')
def index():
return render_index()
@app.route('/celeb', methods=["POST"])
def celeb():
print("=== In /celeb ===")
# Fetch form data
celebrity = request.form.get("name")
say_what = request.form.get("say")
target_language = request.form.get("language")
temperature = request.form.get("temperature")
top_k = request.form.get("top_k")
top_p = request.form.get("top_p")
save_to_mongodb = request.form.get("save_to_mongodb") == "yes"
print(
f"📥 Input: {celebrity}\n🗣️ Say: {say_what}\n🔠 Language: {target_language}")
print(
f"⚙️ Model parameters\n🌡️ Temperature: {temperature}\n🔝 Top-k: {top_k}\n🔝 Top-p: {top_p}")
print(f"💾 Save to MongoDB: {save_to_mongodb}")
# Check if all fields are filled
if not celebrity or not say_what or not target_language:
return render_index(error="Please fill in all the fields.")
# If say_what is more than 100 words, return an error
if len(say_what.split()) > 100:
return render_index(error="Please keep the message under 100 words.")
model_parameters = {
"temperature": float(temperature),
"top_k": int(top_k),
"top_p": float(top_p)
}
# Get the response
response_text, audio_path = gemini.handler(celebrity, say_what,
model_parameters, target_language,
audio_path="./static/audio/response.mp3")
print(f"📤 Response:\n{response_text}")
# Convert the audio to base64
audio_base64 = get_audio_base64(audio_path)
os.remove(audio_path) # audio file not needed anymore (we have base64)
# Generate image of celebrity
image_path = pollinations.image_request_handler(
f"Create a detailed illustration of {celebrity}.", width=512, height=512, seed=42, model="flux", save_path="./static/images/pollinations/image-output.jpg")
result = {
"celeb": celebrity,
"say_what": say_what,
"target_language": target_language,
"temperature": model_parameters["temperature"],
"top_k": model_parameters["top_k"],
"top_p": model_parameters["top_p"],
"response": response_text,
"audio_base64": audio_base64,
"image_path": image_path
}
if save_to_mongodb:
result["mongodb_id"] = str(save_response_to_mongodb(result))
print("✅ Saved the generated response to MongoDB.")
print("✅ Result obtained in /celeb.")
return render_index(result=result)
@app.route("/translate_api", methods=["POST"])
def translate_api():
print("=== In /translate_api ===")
# Get the input data
data = request.get_json()
text = data.get("text")
target_lang = data.get("target_lang")
original_lang = data.get("original_lang")
print(
f"📥 Input text: {text}\nOriginal Language: {original_lang}\nTarget Language: {target_lang}")
# Get translation & audio
translated_text = google_handlers.translate_message(
text, target_lang, source=original_lang)
audio_path = google_handlers.make_audio(
translated_text, target_lang, audio_path="./static/audio/translated.mp3")
# Convert the audio to base64
audio_base64 = get_audio_base64(audio_path)
os.remove(audio_path)
result = {
"translated_text": translated_text,
"audio_base64": audio_base64
}
print("✅ Result obtained in /translate_api.")
return jsonify(result)
@app.route("/fine_tune_api", methods=["POST"])
def fine_tune_api():
print("=== In /fine_tune_api ===")
# Get the input data
data = request.get_json()
text = data.get("text")
fine_tune = data.get("fine_tune")
original_lang = data.get("original_lang")
print(
f"📥 Input text: {text}\nFine-tune: {fine_tune}\nOriginal Language: {original_lang}")
# Call Gemini API to fine-tune the text
fine_tuned_text = gemini.fine_tune(text, fine_tune, original_lang)
# Get audio
audio_path = google_handlers.make_audio(
fine_tuned_text, original_lang, audio_path="./static/audio/fine_tuned.mp3")
# Convert the audio to base64
audio_base64 = get_audio_base64(audio_path)
os.remove(audio_path)
result = {
"fine_tuned_text": fine_tuned_text,
"audio_base64": audio_base64
}
print("✅ Result obtained in /fine_tune_api.")
return jsonify(result)
@app.route("/generate_pdf", methods=["POST"])
def generate_pdf():
print("=== In /generate_pdf ===")
# Get the input data
data = request.get_json()
celeb = data.get("celeb")
say_what = data.get("say_what")
target_language = data.get("target_language")
temperature = data.get("temperature")
top_k = data.get("top_k")
top_p = data.get("top_p")
response = data.get("response")
image_path = data.get("image_path")
print(f"""📥 Input: {celeb}
🗣️ Say: {say_what}
🔠 Language: {target_language}
🌡️ Temperature: {temperature}
🔝 Top-k: {top_k}
🔝 Top-p: {top_p}
📤 Response: {response}
🖼️ Image path: {image_path}""")
# Generate the PDF
pdf_url = pdf_handler.generate_pdf(celeb, say_what, target_language,
temperature, top_k, top_p, response, image_path)
result = {
"pdf_url": pdf_url
}
print("✅ Result obtained in /generate_pdf.")
return jsonify(result)
@app.route("/delete_all_responses", methods=["POST"])
def delete_all_responses():
"""
Delete all the responses saved in the MongoDB collection.
Returns:
- Renders the index page with a success message.
"""
print("=== In /delete_all_responses ===")
# Delete all documents from the collection
mongo_collection.delete_many({})
print("✅ Deleted all responses from MongoDB.")
return render_index(success="All responses have been deleted.")
if __name__ == "__main__":
app.run(debug=True)