-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_gradio.py
487 lines (253 loc) · 9.47 KB
/
main_gradio.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import gradio as gr
from fastapi import Response, Depends, Request, HTTPException
from fastapi.security import OAuth2PasswordRequestForm
from starlette.responses import RedirectResponse
from database.db import SessionLocal
from services.llama_services import conversation
from services.audio_services import upload_audio
from services.llama_services import conversations
from services.history_services import all_chats
from services.history_services import continue_chat
from services.history_services import delete_chat
from user import sign_up
from user_login import login
from paystack import pay
from services.user_services import delete
from services.user_services import update
#remaining sign_up, sign_in, pay
from schema.users_shema import user, premium
from schema.users_shema import show_user
import json
import oauth
from fastapi import BackgroundTasks, FastAPI
from fastapi_mail import ConnectionConfig, FastMail, MessageSchema, MessageType
from schema.users_shema import user
import oauth
current_user = None
#WRAPPER FOR SIGN_UP
async def wrapper_sign_up(name, email, password):
background_tasks = BackgroundTasks()
db = SessionLocal()
try:
new_users = user(name=name, email=email, password=password)
await sign_up(new_users, background_tasks, db)
return "Congratulations!! Your account has been created. Verification email sent."
except HTTPException as e:
print(f"An error occurred: {e.detail}")
return f"An error occurred: {e.detail}"
finally:
db.close()
#WRAPPER FOR AUDIO
async def wrapper_user_login(email, password):
global current_user
request = OAuth2PasswordRequestForm
db = SessionLocal()
try:
new_users = request(password=password, username=email)
token = await login(new_users, db)
print(type(token['access_token']))
print(token['access_token'])
actual_token = token['access_token']
print(actual_token)
current_user = actual_token
return "You've signed in sucessfully"
except HTTPException as e:
print(f"An error occurred: {e.detail}")
return f"An error occurred: {e.detail}"
finally:
db.close()
#WRAPPER FOR UPDATE
async def wrapper_update(name, email):
global current_user
print(f"This is the current user = {current_user}")
db = SessionLocal()
try:
new_data = show_user(name=name, email=email)
user = oauth.get_current_user(current_user, db)
result = update(new_data, db, user)
return result
except HTTPException as e:
print(f"An error occurred: {e.detail}")
return f"An error occurred: {e.detail}"
finally:
db.close()
#WRAPPER FOR DELETE
async def wrapper_delete():
global current_user
print(f"This is the current user = {current_user}")
db = SessionLocal()
try:
user = oauth.get_current_user(current_user, db)
result = delete(db=db, current_user=user)
return result
except HTTPException as e:
print(f"An error occurred: {e.detail}")
return f"An error occurred: {e.detail}"
finally:
db.close()
#PUT THE UI in different pages JUst like this
n = None
def continue_conversation(number):
global n
n = number
return n
#WRAPPER FOR AUDIO
async def wrapper_payment(request:gr.Request, amount):
global current_user
print(f"requesting---------{request}")
if amount == 6500:
premiums = premium.MONTHLY
else:
premiums = premium.YEARLY
print(f"This si right{premiums}")
print(f"This si right{type(premiums)}")
print(f"This is the current user = {current_user}")
db = SessionLocal()
try:
user = oauth.get_current_user(current_user, db)
result = await pay(request=request, amount=premiums, db=db, current_user=user)
print(result)
return result
except HTTPException as e:
print(f"An error occurred: {e.detail}")
return f"An error occurred: {e.detail}"
finally:
db.close()
#WRAPPER FOR AUDIO
async def wrapper_upload_audio(file_path):
global current_user
global n
n = None
print(f"This is the current user = {current_user}")
db = SessionLocal()
try:
user = oauth.get_current_user(current_user, db)
transcription_result, summary_data = await upload_audio(file_path, db, user)
title = summary_data["title"]
summary = summary_data["summary"]
# Convert the data to an HTML unordered list
html_content = '<ul>'
html_content += f"<li><h2>{title}</h2><br><br><strong>{summary}</strong></li>"
html_content += '</ul>'
html = f'<div style="max-height: 500px; overflow-y: auto;">{html_content}</div>'
print(html)
return html
except HTTPException as e:
print(f"An error occurred: {e.detail}")
return f"An error occurred: {e.detail}"
finally:
db.close()
#WRAPPER FOR CONVERSATION
def wrapper_conversation(input, history):
global n
db = SessionLocal()
try:
user = oauth.get_current_user(current_user, db)
if n is not None:
print(f"Done{n}")
llama2, response = continue_chat(Audio_id=n, input=input, db=db, current_user=user)
return llama2
else:
llama2, db_history = conversation(input, db, user)
return llama2
except HTTPException as e:
print(f"An error occurred: {e.detail}")
return f"An error occurred: {e.detail}"
finally:
db.close()
def display_chats():
db = SessionLocal()
try:
user = oauth.get_current_user(current_user, db)
response = all_chats(db, user)
# Convert the data to an HTML unordered list
html_content = '<ul>'
for item in response:
audio_id, title = item
html_content += f"<li>ID: {audio_id}, Title: {title}</li>"
html_content += '</ul>'
html = f'<div style="max-height: 500px; overflow-y: auto;">{html_content}</div>'
print(html)
return html
except HTTPException as e:
print(f"An error occurred: {e.detail}")
return f"An error occurred: {e.detail}"
finally:
db.close()
def wrapper_delete_chat(input):
db = SessionLocal()
try:
user = oauth.get_current_user(current_user, db)
result = delete_chat(input, db, user)
return result
except HTTPException as e:
print(f"An error occurred: {e.detail}")
return f"An error occurred: {e.detail}"
finally:
db.close()
#GRADIO UI:
demo = gr.Blocks()
with demo:
gr.Markdown("PROJECT DEMO.")
#login
with gr.Tab("LOGIN PAGE"):
inputs = [
gr.Textbox(label="email"),
gr.Textbox(label="password", type="password")
]
output = gr.Markdown("")
b1 = gr.Button("input number")
b1.click(wrapper_user_login, inputs=inputs, outputs=output)
#signup
with gr.Tab("SIGNUP PAGE"):
inputs = [
gr.Textbox(label="name"),
gr.Textbox(label="email"),
gr.Textbox(label="password", type="password")
]
output = gr.Markdown("")
b1 = gr.Button("input number")
b1.click(wrapper_sign_up, inputs=inputs, outputs=output)
#chat and audio
with gr.Tab("UPLOAD AUDIO AND CHAT"):
#audio record/upload
audio_file = gr.Audio(type="filepath")
text = gr.HTML(visible = True)
b1 = gr.Button("upload audio")
toggle_text = gr.Button("Collapse")
b1.click(wrapper_upload_audio, inputs=audio_file, outputs=text,)
toggle_text.click(lambda: not text.visible, inputs=[], outputs=text)
#chat
gr.ChatInterface(wrapper_conversation)
with gr.Tab("OPTIONS"):
#all chats
gr.Interface(fn=display_chats, inputs=None, outputs=gr.HTML(label="View chat history"))
#continue chat
audio_file = gr.Textbox(label="continue which chat?")
output = gr.Markdown("")
b1 = gr.Button("input number")
b1.click(continue_conversation, inputs=audio_file, outputs=output)
#Deleting UI
audio_file = gr.Textbox(label="Delete which chat?")
text = gr.Markdown("")
b2 = gr.Button("input number")
b2.click(wrapper_delete_chat, inputs=audio_file, outputs=text)
with gr.Tab("SETTINGS"):
#payments:
inputs=gr.Radio(label="Choose plan and enjoy", choices=[66000, 6500])
redirect_button = gr.Button("thank you!")
link = gr.Markdown("click the url when is shows up to make payment")
redirect_button.click(wrapper_payment, inputs=inputs, outputs=link)
#update user
inputs = [
gr.Textbox(label="name"),
gr.Textbox(label="email"),
]
output = gr.Markdown("")
b2 = gr.Button("update")
b2.click(wrapper_update, inputs=inputs, outputs=output)
#Deleting User
text = gr.Markdown("Parmanently delete your account?")
b3 = gr.Button("Delete your account")
b3.click(wrapper_delete, inputs=None, outputs=text)
demo.launch(share=True)