File size: 23,940 Bytes
9d3162f
 
 
 
 
 
 
 
1438a5c
 
9d3162f
 
 
 
 
 
 
 
33b57a9
9d3162f
 
 
 
 
 
 
 
 
 
 
 
1a27815
a350783
1a27815
33b57a9
9d3162f
 
 
a350783
1a27815
 
9d3162f
 
 
33b57a9
9d3162f
 
 
 
 
 
 
 
 
 
 
1438a5c
 
 
 
 
 
b2e71f3
 
 
d271195
4915e38
 
 
1669617
a350783
 
9d3162f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a27815
8951a9e
 
 
 
 
 
 
 
2d6e5da
8951a9e
 
 
 
 
 
 
 
1a27815
9d3162f
 
 
 
15dd0ab
 
 
9d3162f
 
 
 
 
 
 
 
 
1a27815
 
 
 
33b57a9
e3e6ae0
 
1a27815
 
 
 
9d3162f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115c701
9d3162f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1438a5c
 
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
import asyncio
import base64
import itertools
import json
import os
import shutil
import subprocess
import time
import torch
import uvicorn
import cv2
import numpy as np
import pandas_datareader as pdr
import pytesseract
import speech_recognition as sr
import uvicorn
from fastapi import FastAPI, File, UploadFile,Depends, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse,StreamingResponse
from tqdm import tqdm
from transformers import pipeline

from CaesarDetectEntity import CaesarDetectEntity
from CaesarFaceDetection.caesarfd import CaesarFaceDetection
from CaesarHotelBooking.caesarhotelbooking import CaesarHotelBooking
from CaesarObjectDetection.CaesarYolo import CaesarYolo
from CaesarTranslate import CaesarLangTranslate
from CaesarVoice import CaesarVoice
from csv_to_db import ImportCSV
from RequestModels import *
from CaesarFaceRecognition.caesardeepface import CaesarDeepFace
from CaesarAIART.caesaraiart import CaesarAIART
from CaesarHandwriting.caesarhandwriting import CaesarHandWriting
#from CaesarAIMusicLoad.caesaraimusicload import CaesarAITelegramBOT

importcsv  = ImportCSV("CaesarAI")
caesaryolo = CaesarYolo()
caesarfacedetectmodel = CaesarFaceDetection()
caesarhandwriting = CaesarHandWriting()
#caesartelgegrambot = CaesarAITelegramBOT()

app = FastAPI()

CURRENT_DIR = os.path.realpath(__file__).replace(f"/main.py","")
#caesaraiart = CaesarAIART(CURRENT_DIR)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # can alter with time
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
@app.get("/")
def caesaraihome():
    return "Welcome to CaesarAI's API's and CaesarAINL."
@app.get("/testgpu")
def caesaraihome():
    if torch.cuda.is_available():
        return "gpu is being used."
    else:
        return "gpu is not being used."
@app.get("/caesarailogo")
def caesarailogo():
    return FileResponse(f"{CURRENT_DIR}/CaesarAILogo.png")

@app.post("/caesarhandwriting")
def caesaraihandwriting(urljson : CaesarAIHandwritingModel):
    urljson = dict(urljson)
    text = caesarhandwriting.translate(urljson["message"])
    return {"message":text}


@app.websocket("/caesarobjectdetectws")
async def caesarobjectdetectws(websocket: WebSocket):
    # listen for connections
    await websocket.accept()

    try:
        while True:
            contents = await websocket.receive_bytes()
            arr = np.frombuffer(contents, np.uint8) # turns the image byte data into numpy array

            frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) # turns numpy array into the original image shape and state
            
            image,_,_ =  caesaryolo.caesar_object_detect(frame) # Does object detection and returns a numpy array
            ret, buffer = cv2.imencode('.png', image) # turns numpy array into buffer

            await websocket.send_bytes(buffer.tobytes()) # sends the buffer as bytes


    except WebSocketDisconnect:
        print("Client disconnected")

@app.websocket("/caesarfacedetectws")
async def caesarfacedetect(websocket: WebSocket):
    # listen for connections
    await websocket.accept()

    try:
        while True:
            contents = await websocket.receive_bytes()
            arr = np.frombuffer(contents, np.uint8) # turns the image byte data into numpy array

            frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) # turns numpy array into the original image shape and state
            
            image =  caesarfacedetectmodel.detect_face(frame) # Does object detection and returns a numpy array
            ret, buffer = cv2.imencode('.png', image) # turns numpy array into buffer

            await websocket.send_bytes(buffer.tobytes()) # sends the buffer as bytes


    except WebSocketDisconnect:
        print("Client disconnected")
"""
@app.get("/getcaesartelegramchatids")
def getcaesartelegramchatid():
    try:
        chatids = caesartelgegrambot.get_chatids()
        return chatids
    except Exception as ex:
        return {"error":f"{type(ex)},{ex}"}
@app.get("/caesartelegramsendmessage")
def caesartelegramsendmessage(messageinfo: CaesarTelegramMessageModel):
    try:
        messageinfo = dict(messageinfo)
        chat_id = messageinfo["chat_id"]
        message = messageinfo["message"]
        chatids = caesartelgegrambot.send_message(chat_id,message)
        return chatids
    except Exception as ex:
        return {"error":f"{type(ex)},{ex}"}
"""
@app.post("/caesarfacesnap")
def caesarfacesnap(frames: CaesarOCRHTTPModel):
    try:
        frames = dict(frames)
        image_arr = np.frombuffer(base64.b64decode(frames["frame"]),dtype="uint8")
        image = cv2.imdecode(image_arr,flags=cv2.IMREAD_COLOR)
        #.reshape(frames["shape"][0],frames["shape"][1],3)
        image = caesarfacedetectmodel.detect_face(image,snapcropface=True)
        if image == [] or image is None:
            return {"frame":"no face was detected."}
        elif image != [] or image is None:
            x=np.ascontiguousarray(image)
            return {'frame': base64.b64encode(x).decode(),"shape":[image.shape[0],image.shape[1]]}
    except Exception as ex:
        return {"error":f"{type(ex)},{ex}"}

@app.post("/caesaraiart")
def caesarart(promptjson: CaesarAIARTModel):
    try:
        promptjson = dict(promptjson)
        image = CaesarAIART.generate_api(promptjson["prompt"])
        return StreamingResponse(content=image, media_type="image/png")
        #return StreamingResponse(image, media_type="image/png")#FileResponse(f"{CURRENT_DIR}/CaesarAIART/caesarart.png")

    except Exception as ex:
        return {"error":f"{type(ex)},{ex}"}

@app.post("/faceauthorizeuser")
def faceauthorizeuser(recogperson: CaesarFaceAuth = Depends(),file: UploadFile = File(...)):

    try:
        recogperson = dict(recogperson)
        auth_file = f"{CURRENT_DIR}/CaesarFaceRecognition/authimages/{recogperson['user']}.jpg"
        with open(auth_file, 'wb') as f:
            shutil.copyfileobj(file.file, f)
        return {"message":f"{recogperson['user']} is authorized for face recognition."}
    except Exception as ex:
        return {"message": f"{type(ex)},{ex}"}
    finally:
        file.file.close()
# https://stackoverflow.com/questions/65504438/how-to-add-both-file-and-json-body-in-a-fastapi-post-request
@app.post("/caesarfaceauth")
def caesarfaceauth(recogperson: CaesarFaceAuth = Depends(),file: UploadFile = File(...)):

    try:
        recogperson = dict(recogperson)
        uploaded_file = f"{CURRENT_DIR}/CaesarFaceRecognition/images/temp.jpg"
        auth_file = f"{CURRENT_DIR}/CaesarFaceRecognition/authimages/{recogperson['user']}.jpg"
        with open(uploaded_file, 'wb') as f:
            shutil.copyfileobj(file.file, f)
        caeesardeepface = CaesarDeepFace()
        result = caeesardeepface.face_authentication(filename1=auth_file,filename2=uploaded_file)
        return result
    except Exception as ex:
        return {"message": f"{type(ex)},{ex}"}
    finally:
        file.file.close()

        
   

@app.websocket("/caesarocrextractionws")
async def caesarocrextractionws(websocket: WebSocket):
    # listen for connections
    await websocket.accept()

    try:
        while True:
            contents = await websocket.receive_bytes()
            target_words = await websocket.receive_json()
            
            target_words = dict(target_words)["target_words"]


            arr = np.frombuffer(contents, np.uint8) # turns the image byte data into numpy array

            frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) # turns numpy array into the original image shape and state
            
            data = pytesseract.image_to_data(frame, output_type=pytesseract.Output.DICT)
            image_copy = frame.copy()

            # get all data from the image
            data = pytesseract.image_to_data(frame, output_type=pytesseract.Output.DICT)

            # print the data
            #print(data["text"])

            # get all occurences of the that word
            word_occurences = [ i for i, word in enumerate(data["text"]) for target in target_words if word.lower() == target]

            for occ in word_occurences:
                # extract the width, height, top and left position for that detected word
                w = data["width"][occ]
                h = data["height"][occ]
                l = data["left"][occ]
                t = data["top"][occ]
                # define all the surrounding box points
                p1 = (l, t)
                p2 = (l + w, t)
                p3 = (l + w, t + h)
                p4 = (l, t + h)
                # draw the 4 lines (rectangular)
                image_copy = cv2.line(image_copy, p1, p2, color=(255, 0, 0), thickness=2)
                image_copy = cv2.line(image_copy, p2, p3, color=(255, 0, 0), thickness=2)
                image_copy = cv2.line(image_copy, p3, p4, color=(255, 0, 0), thickness=2)
                image_copy = cv2.line(image_copy, p4, p1, color=(255, 0, 0), thickness=2)

            ret, buffer = cv2.imencode('.png', image_copy) # turns numpy array into buffer

            await websocket.send_bytes(buffer.tobytes()) # sends the buffer as bytes


    except WebSocketDisconnect:
        print("Client disconnected")

@app.websocket("/caesarocrws")
async def caesarocrws(websocket: WebSocket):
    # listen for connections
    await websocket.accept()

    try:
        while True:
            contents = await websocket.receive_bytes()

            arr = np.frombuffer(contents, np.uint8) # turns the image byte data into numpy array

            frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) # turns numpy array into the original image shape and state
            # get the string
            string = pytesseract.image_to_string(frame)
            # print it
            #print(string)

            

            message = json.dumps({"message":string})
            ret, buffer = cv2.imencode('.png', frame) # turns numpy array into buffer

            await websocket.send_json(message) # sends the buffer as bytes
            await websocket.send_bytes(buffer.tobytes()) # sends the buffer as bytes



    except WebSocketDisconnect:
        print("Client disconnected")
@app.post("/caesarocr")
def caesarocr(frames: CaesarOCRHTTPModel):
    # listen for connections
    try:
        frames = dict(frames)
        image_arr = np.frombuffer(base64.b64decode(frames["frame"].replace("data:image/jpeg;base64,","").replace("data:image/png;base64,","")),dtype="uint8")
        image = cv2.imdecode(image_arr,flags=cv2.IMREAD_COLOR)
        # .reshape(frames["shape"][0],frames["shape"][1],3)
        string = pytesseract.image_to_string(image)
        return {'message': string}
    except Exception as ex:
        return {"error":f"{type(ex)},{ex}"}
        
@app.websocket("/sendvideows")
async def sendvideows(websocket: WebSocket):
    # listen for connections
    await websocket.accept()

    try:
        while True:
            contents = await websocket.receive_bytes()
            arr = np.frombuffer(contents, np.uint8)
            #print(arr)
            
            frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED)
            #print(frame.shape)
            #image =  caesaryolo.caesar_object_detect(frame)
            ret, buffer = cv2.imencode('.png', frame)
            #print(buffer)
            await websocket.send_bytes(buffer.tobytes())


    except WebSocketDisconnect:
        print("Client disconnected")
# Done
@app.post("/caesarobjectdetect")
def caesarobjectdetect(frames: CaesarObjectDetectModel):
    frames = dict(frames)
    image,_,_ = caesaryolo.caesar_object_detect(np.frombuffer(base64.b64decode(frames["frame"]),dtype="uint8").reshape(frames["shape"][0],frames["shape"][1],3))#base64.b64decode(frames["frame"]))
    return {'frame': base64.b64encode(image).decode(),"shape":[image.shape[0],image.shape[1]]}

@app.websocket("/caesarobjectdetectvideows")
async def caesarobjectdetectvideows(websocket: WebSocket):

    # listen for connections
    await websocket.accept()

    try:
        while True:
            video_frames = await websocket.receive_json()
            with open(f'{CURRENT_DIR}/CaesarObjectDetection/content.mp4', 'wb') as f:
                f.write(base64.b64decode(video_frames["frame"].replace("data:video/mp4;base64,","")))
            duration = 20 # seconds
            best_min_size = (32000 + 100000) * (1.073741824 * duration) / (8 * 1024)
            CaesarYolo.compress_video(f"{CURRENT_DIR}/CaesarObjectDetection/content.mp4", f'{CURRENT_DIR}/CaesarObjectDetection/content_compressed.mp4', best_min_size)
            if "content.mp4" in os.listdir(f"{CURRENT_DIR}/CaesarObjectDetection"):
                os.remove(f"{CURRENT_DIR}/CaesarObjectDetection/content.mp4")
            caesaryolo.video_load('content.mp4')
            while True:
                image,time_elapsed,end_time = caesaryolo.caesar_object_detect("video")
                if image is not None:
                    #print(image)
                    ret, buffer = cv2.imencode('.png', image) # turns numpy array into buffer

                    #png_as_text = base64.b64encode(buffer)

                    #await websocket.send_bytes(base64.b64encode(buffer).decode("utf-8"))
                    await websocket.send_json({"time_elapsed":time_elapsed,"end_time":end_time})
                    #cv2.imshow("image", image)
                
                else:
                    break
            


            caesaryolo.cap.release()
            ffmpeg_command = ["ffmpeg","-y","-i",f"{CURRENT_DIR}/CaesarObjectDetection/output.avi",f"{CURRENT_DIR}/CaesarObjectDetection/output.mp4"]
            subprocess.call(ffmpeg_command)
            await websocket.send_json({"result":"finished"})


    except WebSocketDisconnect:
        print("Client disconnected")

# Done
@app.get("/caesarobjectdetectvideoget")
def caesarobjectdetectvideoget():
    return FileResponse(f"{CURRENT_DIR}/CaesarObjectDetection/output.mp4")

@app.post("/createcaesaraipi")
def createcaesaraipi(caesar_api_post : CaesarCreateAPIModel):
    caesar_api_post = dict(caesar_api_post)
    caesarapi_db_exists = list(importcsv.db.caesarapis.find( { "caesarapis" : { "$exists" : "true" } } ))
    #print(caesarapi_db)
    if len(caesarapi_db_exists) == 0:
        importcsv.db.caesarapis.insert_one(caesar_api_post)
        return {"message":"caesarapi created."}
    elif len(caesarapi_db_exists) > 0:
        caesarapi_db = caesarapi_db_exists[0]
        #print(caesarapi_db)
        for apis in caesar_api_post["caesarapis"]:
            if apis not in caesarapi_db["caesarapis"]:
                caesarapi_db["caesarapis"].append(apis)
            elif apis in caesarapi_db["caesarapis"]:
                continue
        importcsv.db.caesarapis.replace_one({ "caesarapis" : { "$exists" : "true" } },caesarapi_db)
        return {"message":"caesarapi stored."}
# Done
@app.get("/getcaesaraipi")
def getcaesaraipi():
    try:
        caesarapi_db_exists = list(importcsv.db.caesarapis.find( { "caesarapis" : { "$exists" : "true" } } ))[0]
        del caesarapi_db_exists["_id"]
        return caesarapi_db_exists
    except KeyError as kex:
        return {"error":f"Api doesn't exist"}
# Done
@app.post("/triggerapi")
def triggerapi(user_trigger : TriggerAPIModel):
    user_trigger = dict(user_trigger)["user_trigger"]
    try:
        caesarapi_db_exists = list(importcsv.db.caesarapis.find( { "caesarapis" : { "$exists" : "true" } } ))[0]
    except KeyError as kex:
        return {"error":"Api doesn't exist"}
    try:
        triggered_apis = [i for i in caesarapi_db_exists["caesarapis"] if i["triggerwords"] in user_trigger]
        triggered_api = triggered_apis[0]
        return triggered_api
    except (IndexError,KeyError) as kex:
        return {"message":"sorry couldn't understand what api you want."}
# Done
@app.post("/caesaraihotelbookings")
def caesaraihotelbookings(hotelbookings_json: CaesarHotelBookingsModel):
    """
    params = {
    "city":city,
    "checkin_date":"2023-8-01",
    "checkout_date":"2023-8-08",
    "purpose":"work",
    "num_of_adults":10,
    "num_of_rooms":5,
    "num_of_children":0,
    "page_num":i
    }
    """
    def get_price_range(bookings_json,city,range,):
        def condition(dic):
            ''' Define your own condition here'''
            try:
                price = dic['assumed_final_price']
                return price <= range
            except KeyError as kex:
                return False
        #print(bookings_json)
        bookings = bookings_json[f"{city.lower()}_bookings"]
        filtered = [d for d in bookings if condition(d)]
        return filtered
        
    
    try:
        overall_booking_info = []
        hotelbookings_json = dict(hotelbookings_json)
        try:
            exclude_whole = hotelbookings_json["exclude_whole"]
        except KeyError as kex:
            exclude_whole = None
        city = hotelbookings_json["city"]
        price_range = hotelbookings_json["price_range"]
        print(f"Extracting flight data for {city}...")
        for i in tqdm(range(1,hotelbookings_json["num_of_pages"]+1)):
            params = {
            "city":city,
            "checkin_date":hotelbookings_json["checkin_date"],
            "checkout_date":hotelbookings_json["checkout_date"],
            "purpose":hotelbookings_json["purpose"],
            "num_of_adults":hotelbookings_json["num_of_adults"],
            "num_of_rooms":hotelbookings_json["num_of_rooms"],
            "num_of_children":hotelbookings_json["num_of_children"],
            "page_num":i
            }
            url = CaesarHotelBooking.create_url(**params)
            bookinginfo = CaesarHotelBooking.caesar_get_hotel_info(url)
            overall_booking_info.append(bookinginfo)
        full_bookings = list(itertools.chain(*overall_booking_info))
        price_range_bookings = get_price_range({f"{city.lower()}_bookings":full_bookings},city,price_range)
        if exclude_whole == "true":
            return {"caesaroutput":{"caesarbookings":price_range_bookings}}
            #return {f"{city.lower()}_bookings_lower_than_{price_range}":price_range_bookings}
        return {"caesaroutput":{"caesarbookings":full_bookings}}
        #return {"caesaroutput":full_bookings}
        #return {f"{city.lower()}_bookings":full_bookings,f"{city.lower()}_bookings_lower_than_{price_range}":price_range_bookings}
    except Exception as ex:
        return {"error":f"{type(ex)}{ex}"}

@app.post("/caesarlangtranslate")
def caesarlangtranslate(translate_json: CaesarLangTranslateModel):
    try:# hello
        translate_json = dict(translate_json)
        text = translate_json["caesartranslate"]

        language = "en"
        try:
            responsejson = translate_json["response"]
            language =  translate_json["language"]
            try:
                triggerword  = translate_json["triggerword"]
                caesarlang = CaesarDetectEntity()
                text,language = caesarlang.run(triggerword,text,caesarlang.main_entities[triggerword])
            except KeyError as kex:
                pass
            if responsejson == "true":
                response = True
            elif responsejson == "false":
                response = False
            else:
                response = False
        except KeyError as kex:
            response = False
        caesarlangtranslate = CaesarLangTranslate()
        original,translation,original_language,destination_language = caesarlangtranslate.translate(text,lang=language,response=response)


        return {"caesaroutput":translation,"caesartranslation":{"original":original,"translation":translation,"original_language":original_language,"destination_language":destination_language}}

    except Exception as ex:
        return {"error":f"{type(ex)}-{ex}"}




@app.get("/caesarlangtranslate")
def caesarlangtranslateget(text:str,triggerword:str,responsejson:str):
    try:# hello 
        caesarlang = CaesarDetectEntity()
        try:
            text,language = caesarlang.run(triggerword,text,caesarlang.main_entities[triggerword])
            if responsejson == "true":
                response = True
            elif responsejson == "false":
                response = False
            else:
                response = False
        except KeyError as kex:
            response = False
        caesarlangtranslate = CaesarLangTranslate()
        original,translation,original_language,destination_language = caesarlangtranslate.translate(text,lang=language,response=response)


        return {"caesaroutput":translation,"caesartranslation":{"original":original,"translation":translation,"original_language":original_language,"destination_language":destination_language}}

    except Exception as ex:
        return {"error":f"{type(ex)}-{ex}"}


@app.post("/caesarsr")
def caesarsr(audio_data: CaesarSRModel):

    transcript = ""
    try:
        #print("FORM DATA RECEIVED")
        
        # TODO Make it show that the .3pg extension is adaptable
        audiobase64json = dict(audio_data)
        decode_bytes = base64.b64decode(audiobase64json["audio_data"].replace("data:video/3gpp;base64,",""))
        with open("temp.3pg", "wb") as wav_file:
            wav_file.write(decode_bytes)
        
        os.system('ffmpeg -y -i temp.3pg understand.wav')
        recognizer = sr.Recognizer()
        audioFile = sr.AudioFile("understand.wav")
        with audioFile as source:
            data = recognizer.record(source)
        transcript = recognizer.recognize_google(data, key=None)
        return {"message":transcript}

    except Exception as ex:
        return {"error":f"{type(ex)}-{ex}"}


@app.post("/caesarvoice")
def caesarvoice(voice_input : CaesarVoiceModel):
    try:
        voice_input = dict(voice_input)
        #try:
        filename = voice_input["filename"]
        lang = voice_input["language"]
        if filename == None:
            filename = "temp.wav"
        if lang == None:
            lang = "en"
        CaesarVoice.synthesise(voice_input["text"],filename,lang=lang)
        return {"message":"voice syntheized"}# send_file(filename,"audio/x-wav")
    except Exception as ex:
        return {"error":f"{type(ex)}-{ex}"}

@app.post("/caesarsummarize")
def caesarsummarize(json_input : CaesarSummarizeModel):
    try:
        json_input = dict(json_input)
        original_text = json_input["text"]
        summarization = pipeline("summarization")
        summary_text = summarization(original_text)[0]['summary_text']
        return {"caesaroutput":summary_text}# send_file(filename,"audio/x-wav")
    except Exception as ex:
        return {"error":f"{type(ex)}-{ex}"}

@app.post("/caesarstockinfo")
def caesarstockinfo(json_input: CaesarStockInfoModel):
    try:
        json_input = dict(json_input)
        # import AAPL stock price
        stock_tick = json_input["stock"]
        start_date = json_input["start_date"]
        end_date =  json_input["end_date"]
        df = pdr.get_data_yahoo(stock_tick, start=start_date,end=end_date)
        print(df)
        return {"caesaroutput":str(df.to_csv(index=False))}# send_file(filename,"audio/x-wav")
    except Exception as ex:
        return {"error":f"{type(ex)}-{ex}"}




@app.get("/caesarvoiceget")
def caesarvoiceget():
    try:
        filename = "temp.wav"
        return FileResponse(filename,media_type="audio/x-wav")
    except Exception as ex:
        return {"error":f"{type(ex)}-{ex}"}
async def main():
    config = uvicorn.Config("main:app", port=7860, log_level="info",host="0.0.0.0",reload=True)
    server = uvicorn.Server(config)
    await server.serve()

if __name__ == "__main__":
    #asyncio.run(main())
    uvicorn.run("main:app",port=7860, log_level="info",host="0.0.0.0",reload=True)