-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflask_server.py
More file actions
1047 lines (785 loc) · 42.2 KB
/
flask_server.py
File metadata and controls
1047 lines (785 loc) · 42.2 KB
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from config import *
from flask import Flask, jsonify, request
import google.generativeai as genai
from dotenv import load_dotenv
from flask_cors import CORS
from PIL import Image
import requests
import base64
import json # for image classification outputg
# Define your flask server
app = Flask()
#! ------------------------------------------------------------------------------------------------------
#! # Text GENERATION
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Model : gemini (Base Model)
# Speciality: Base Model
# prompt : {"prompt":"Say Unes is COooOoOoL"}
# =================================================================================================
@app.route('/text_generation/gemini', methods=['POST'])
def generate_text_gemini():
try:
data = request.json
role = data["role"]
prompt = data["prompt"]
result = core.text_generation(role,prompt)
return jsonify({"status": "success", "result": result})
except Exception as e:
return jsonify({"status": "error", "message": f"Error generating text: {str(e)}"}),500
#! ------------------------------------------------------------------------------------------------------
#! # Gemini Vision
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Model : gemini_vision (Base Model)
# Speciality: Base Model
# prompt : {"prompt":"WHO IS THIS","role":"you are Unes Fan","images":[image1,Image2]}
# =================================================================================================
@app.route('/image2txt/gemini_vision', methods=['POST'])
def image2text_gemini():
try:
if 'images' in request.files and 'prompt' in request.form and 'role' in request.form:
image_file = request.files['images']
prompt = request.form['prompt']
role = request.form['role']
data = {"prompt": prompt, "role": role}
app.logger.info(f"Received data: {data}")
result = core.gemini_img2txt(data, image_file)
return jsonify({"status": "success", "result": result})
else:
return jsonify({"status": "error", "message": "Invalid request format"}), 400
except Exception as e:
app.logger.error(f"Error processing request: {str(e)}")
return jsonify({"status": "error", "message": f"Error generating vision image: {str(e)}"}), 500
#! ------------------------------------------------------------------------------------------------------
#! # TEXT TO IMAGE
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Model : SSD_1B_ANIME (Base Model)
# Speciality: Base Model
# Prompt : {"prompt":"NEON CAT"}
# =================================================================================================
@app.route("/txt2img/SSD_1B_ANIME", methods=["POST"])
def text2image_SSD_1B_ANIME():
try:
data = request.get_json()
user_input = data.get("prompt") # you can include a default prompt
image_bytes = core.text2image({
"inputs": user_input,
},txt2img_SDD_1B_ANIME_api_token)
# Convert binary data to Base64-encoded string
base64_encoded_image = base64.b64encode(image_bytes).decode('utf-8')
# Return a successful response
return jsonify({"status": "success", "image": base64_encoded_image})
except Exception as e:
# Return an error response if an exception occurs
return jsonify({"status": "error", "message": f"Error generating image: {str(e)}"}),500
# =================================================================================================
# Model : SSD_1B (Base Model)
# Speciality: Base Model
# Prompt : {"prompt":"NEON CAT"}
# =================================================================================================
@app.route("/txt2img/SSD_1B", methods=["POST"])
def text2image_SSD_1B():
try:
data = request.get_json()
user_input = data.get("prompt") # you can include a default prompt
image_bytes = core.text2image({
"inputs": user_input,
},txt2img_SDD_1B_api_token)
# Convert binary data to Base64-encoded string
base64_encoded_image = base64.b64encode(image_bytes).decode('utf-8')
# Return a successful response
return jsonify({"status": "success", "image": base64_encoded_image})
except Exception as e:
# Return an error response if an exception occurs
return jsonify({"status": "error", "message": f"Error generating image: {str(e)}"}),500
# =================================================================================================
# Model : OPENDALLE-V1 (Base Model)
# Speciality: Base Model
# Prompt : {"prompt":"NEON CAT"}
# =================================================================================================
@app.route("/txt2img/OPENDALLE", methods=["POST"])
def text2image_OPENDALLE():
try:
data = request.get_json()
user_input = data.get("prompt")
image_bytes = core.text2image({
"inputs": user_input,
},txt2img_OPENDALLE_api_token)
# Convert binary data to Base64-encoded string
base64_encoded_image = base64.b64encode(image_bytes).decode('utf-8')
# Return a successful response
return jsonify({"status": "success", "image": base64_encoded_image})
except Exception as e:
# Return an error response if an exception occurs
return jsonify({"status": "error", "message": f"Error generating image: {str(e)}"}),500
# =================================================================================================
# Model : OPENDALLE-V1 (EXAMPLE)
# Speciality: Generates Content based on this prompt: Ultra Realistic, Neon Lightning , 16K, Face Focus , Anime Picture , Smooth Lightning
# Prompt : {"prompt":"NEON CAT"}
# =================================================================================================
@app.route("/txt2img/OPENDALLE/Speciality", methods=["POST"])
def text2image_OPENDALLE_Speciality_1():
try:
data = request.get_json()
user_input = data.get("prompt") # you can include a default prompt
image_bytes = core.text2image({
"inputs": user_input,
},txt2img_OPENDALLE_api_token)
# Convert binary data to Base64-encoded string
base64_encoded_image = base64.b64encode(image_bytes).decode('utf-8')
# Return a successful response
return jsonify({"status": "success", "image": base64_encoded_image})
except Exception as e:
# Return an error response if an exception occurs
return jsonify({"status": "error", "message": f"Error generating image: {str(e)}"}),500
#! ------------------------------------------------------------------------------------------------------
#! # IMAGE TO TEXT
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Model : BLIP_IMAGE_CAPTIONING_LARGE
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/img2txt/BLIP_IMAGE_CAPTIONING_LARGE', methods=['POST'])
def image2text_BLIP():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
if file:
try:
data = file.read()
result = core.image2text(data,img2txt_BLIP_api_token)
return jsonify({"status": "success", "text": result[0]["generated_text"]})
except Exception as e:
return jsonify({"status": "error", "message": f"Error generating image: {str(e)}"}),500
#! ------------------------------------------------------------------------------------------------------
#! # AUDIO TO TEXT
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Model : BLIP_IMAGE_CAPTIONING_LARGE
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/audio2txt/WHISPER_LARGE_V2', methods=['POST'])
def audio2text_WHISPER():
try:
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
output = core.audio2text(file)
return jsonify({"status": "success", "text":output})
except Exception as e:
print(e)
return jsonify({"status": "error", "message": f"Error generating text: {str(e)}"}),500
#! ------------------------------------------------------------------------------------------------------
#! # Text to audio
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Model : MMS_TTS_ENGs
# Speciality: Base Model
# prompt : {"prompt":"hello world!"}
# =================================================================================================
@app.route('/txt2audio/MMS_TTS_ENG', methods=['POST'])
def text2audio_MMS_TSS_ENG():
try:
data = request.get_json()
text_input = data.get('prompt')
audio_bytes = core.text2audio(text_input,txt2audio_MMS_TTS_ENG_api_token)
return jsonify({'audio': base64.b64encode(audio_bytes).decode('utf-8')})
except Exception as e:
print(e)
return jsonify({'error': str(e)}), 500
#! ------------------------------------------------------------------------------------------------------
#! # Image to Audio
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Models : BLIP_IMAGE_CAPTIONING_LARGE + MMS_TTS_ENG
# Speciality: Base Models
# prompt : {"file":file}
# =================================================================================================
def query_huggingface_api(data):
response = requests.post(img2txt_BLIP_api_token, headers=headers, data=data)
return response.json()
@app.route('/img2audio/MMS_BLIP', methods=['POST'])
def image2audio_MMS_BLIP():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
if file:
try:
data = file.read()
result = query_huggingface_api(data)
audio_bytes = core.text2audio(result[0]["generated_text"],txt2audio_MMS_TTS_ENG_api_token)
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
return jsonify({"status": "success", "audio": audio_base64})
except Exception as e:
print(e)
return jsonify({"status": "error", "message": f"Error generating audio: {str(e)}"}),500
#! ------------------------------------------------------------------------------------------------------
#! # Audio to Image
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Models : WHISPER + OPENDALLE (base Model)
# Speciality: Base Models
# prompt : {"file":file}
# =================================================================================================
@app.route('/audio2img/WHISPER_OPENDALLE', methods=['GET', 'POST'])
def audio2img_WHISPER_OPENDALLE():
if request.method == 'POST':
try:
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
text = core.audio2text(file)
image_bytes = core.text2image({
"inputs": text,
},txt2img_OPENDALLE_api_token)
base64_encoded_image = base64.b64encode(image_bytes).decode('utf-8')
return jsonify({"status": "success", "Image": base64_encoded_image})
except Exception as e:
print(e)
return jsonify({'error': str(e)}), 500
# =================================================================================================
# Models : WHISPER + SDD-1B (base Model)
# Speciality: Base Models
# prompt : {"file":file}
# =================================================================================================
@app.route('/audio2img/WHISPER_SSD_1B', methods=['GET', 'POST'])
def audio2img_WHISPER_SSD_1B():
if request.method == 'POST':
try:
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
text = core.audio2text(file)
image_bytes = core.text2image({
"inputs": text,
},txt2img_SDD_1B_api_token)
base64_encoded_image = base64.b64encode(image_bytes).decode('utf-8')
return jsonify({"status": "success", "Image": base64_encoded_image})
except Exception as e:
print(e)
return jsonify({'error': str(e)}), 500
# =================================================================================================
# Models : WHISPER + SDD-1B-ANIME (base Model)
# Speciality: Base Models
# prompt : {"file":file}
# =================================================================================================
@app.route('/audio2img/WHISPER_SSD_1B_ANIME', methods=['POST'])
def audio2img_WHISPER_SSD_1B_ANIME():
try:
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
text = core.audio2text(file)
image_bytes = core.text2image({
"inputs": text,
},txt2img_SDD_1B_ANIME_api_token)
base64_encoded_image = base64.b64encode(image_bytes).decode('utf-8')
return jsonify({"status": "success", "Image": base64_encoded_image})
except Exception as e:
print(e)
return jsonify({'error': str(e)}), 500
#! ------------------------------------------------------------------------------------------------------
#! # IMAGE CLASSIFICATION
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Models : RESNET (base Model)
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/image_classification/RESNET',methods=["POST"])
def image_classification_RESNET():
# Input Verification
if 'file' not in request.files:
return jsonify({"error":"No file part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"error":"No Selected File"}),400
if file:
try:
image = file.read()
result = core.image_classification(image,img_classification_RESNET_api_token)
parsed_result = json.loads(result)
print(parsed_result)
return jsonify({"status":"success","classes":parsed_result}),200
except Exception as e:
return jsonify({"status":"error","message":str(e)}),500
# =================================================================================================
# Models : VIT_AGE (base Model)
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/image_classification/VIT_AGE',methods=["POST"])
def image_classification_VIT_AGE():
# Input Verification
if 'file' not in request.files:
return jsonify({"error":"No file part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"error":"No Selected File"}),400
if file:
try:
image = file.read()
result = core.image_classification(image,img_classification_VIT_AGE_api_token)
parsed_result = json.loads(result)
print(parsed_result)
return jsonify({"status":"success","classes":parsed_result}),200
except Exception as e:
return jsonify({"status":"error","message":str(e)}),500
# =================================================================================================
# Models : NFWS (base Model)
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/image_classification/NFWS',methods=["POST"])
def image_classification_NFWS():
# Input Verification
if 'file' not in request.files:
return jsonify({"error":"No file part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"error":"No Selected File"}),400
if file:
try:
image = file.read()
result = core.image_classification(image,img_classification_NFWS_api_token)
parsed_result = json.loads(result)
print(parsed_result)
return jsonify({"status":"success","classes":parsed_result}),200
except Exception as e:
return jsonify({"status":"error","message":str(e)}),500
#! ------------------------------------------------------------------------------------------------------
#! # IMAGE SEGMENTATION
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Models : B2_CLOTHES (base Model)
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
#? THERE IS AN ERROR IN THE SCORES (always set to one D:)
@app.route("/image_segmentation/B2_CLOTHES",methods=["POST"])
def image_segmentation_B2_CLOTHES():
if "file" not in request.files:
return jsonify({"status":"error","message":"No File Part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"status":"error","message":"No Selected Files"}),400
if file:
try:
data = file.read()
image_bytes = core.image_segmentation(data,img_segmentation_b2_clothes_api_token)
# Parse the JSON string into a Python list of dictionaries
data_list = json.loads(image_bytes)
scores =[]
labels = []
segmented_pictures=[]
for obj in data_list:
score = obj['score']
label = obj['label']
mask_base64 = obj['mask']
# DO APPEND STUFF :D
scores.append(score)
labels.append(label)
segmented_pictures.append(mask_base64)
return jsonify({"status":"sucess","Segmented Image":{"labels":labels,"scores":scores,"segemented_pictures":segmented_pictures}})
except Exception as e:
return jsonify({"status":"error","message":str(e)})
#! ------------------------------------------------------------------------------------------------------
#! # AUDIO CLASSIFICATION
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Models : Hubert_emotion (base Model)
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/image_classification/Hubert_emotion',methods=["POST"])
def audio_classification_Hubert_emotion():
# Input Verification
if 'file' not in request.files:
return jsonify({"error":"No file part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"error":"No Selected File"}),400
if file:
try:
data = core.audio_classification(file,audio_classification_Hubert_emotion_api_token)
list_data = json.loads(data)
scores = []
labels = []
for obj in list_data:
scores.append(obj["score"])
labels.append(obj["label"])
return jsonify({"status":"success","result":{"scores":scores,"labels":labels}}),200
except Exception as e:
return jsonify({"status":"error","message":str(e)}),500
# =================================================================================================
# Models : wav2vec2_lg_xlsr_en (base Model) Emotions as well :D
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/image_classification/wav2vec2_lg_xlsr_en',methods=["POST"])
def audio_classification_wav2vec2_lg_xlsr_en():
# Input Verification
if 'file' not in request.files:
return jsonify({"error":"No file part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"error":"No Selected File"}),400
if file:
try:
data = core.audio_classification(file,audio_classification_wav2vec2_lg_xlsr_en_api_token)
list_data = json.loads(data)
scores = []
labels = []
for obj in list_data:
scores.append(obj["score"])
labels.append(obj["label"])
return jsonify({"status":"success","result":{"scores":scores,"labels":labels}}),200
except Exception as e:
return jsonify({"status":"error","message":str(e)}),500
# =================================================================================================
# Models : distil_ast_audioset (base Model) IT Detects what the voice is !
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/image_classification/distil_ast_audioset',methods=["POST"])
def audio_classification_distil_ast_audioset():
# Input Verification
if 'file' not in request.files:
return jsonify({"error":"No file part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"error":"No Selected File"}),400
if file:
try:
data = core.audio_classification(file,audio_classification_distil_ast_audioset_api_token)
list_data = json.loads(data)
scores = []
labels = []
for obj in list_data:
scores.append(obj["score"])
labels.append(obj["label"])
return jsonify({"status":"success","result":{"scores":scores,"labels":labels}}),200
except Exception as e:
return jsonify({"status":"error","message":str(e)}),500
# =================================================================================================
# Models : wav2vec2_large_xlsr_53_gender (base Model)
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/image_classification/wav2vec2_large_xlsr_53_gender',methods=["POST"])
def audio_classification_wav2vec2_large_xlsr_53_gender():
# Input Verification
if 'file' not in request.files:
return jsonify({"error":"No file part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"error":"No Selected File"}),400
if file:
try:
data = core.audio_classification(file,audio_classification_wav2vec2_large_xlsr_53_gender_api_token)
list_data = json.loads(data)
scores = []
labels = []
for obj in list_data:
scores.append(obj["score"])
labels.append(obj["label"])
return jsonify({"status":"success","result":{"scores":scores,"labels":labels}}),200
except Exception as e:
return jsonify({"status":"error","message":str(e)}),500
# =================================================================================================
# Models : mms_lid_126 (base Model) For Languages classification
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/image_classification/mms_lid_126',methods=["POST"])
def audio_classification_mms_lid_126():
# Input Verification
if 'file' not in request.files:
return jsonify({"error":"No file part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"error":"No Selected File"}),400
if file:
try:
data = core.audio_classification(file,audio_classification_mms_lid_126_api_token)
list_data = json.loads(data)
scores = []
labels = []
for obj in list_data:
scores.append(obj["score"])
labels.append(obj["label"])
return jsonify({"status":"success","result":{"scores":scores,"labels":labels}}),200
except Exception as e:
return jsonify({"status":"error","message":str(e)}),500
#! ------------------------------------------------------------------------------------------------------
#! # OBJECT DETECTION
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Models : detr_resnet (base Model)
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route("/object_detection/detr_resnet_50",methods=["POST"])
def object_detection_detr_resnet():
if "file" not in request.files:
return jsonify({"status":"error","message":"No File Part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"status":"error","message":"No Selected Files"}),400
if file:
try:
data = file.read()
image_bytes = core.object_detection(data,object_detection_detr_resnet_50_api_token)
# Parse the JSON string into a Python list of dictionaries
data_list = json.loads(image_bytes)
print(data_list)
scores =[]
labels = []
segmented_pictures=[]
for obj in data_list:
score = obj['score']
label = obj['label']
mask_base64 = obj['box']
# DO APPEND STUFF :D
scores.append(score)
labels.append(label)
segmented_pictures.append(mask_base64)
return jsonify({"status":"sucess","Segmented Image":{"labels":labels,"scores":scores,"segemented_pictures":segmented_pictures}})
except Exception as e:
return jsonify({"status":"error","message":str(e)})
# =================================================================================================
# Models : yolos_fashionpedia (base Model)
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route("/object_detection/yolos_fashionpedia",methods=["POST"])
def object_detection_yolos_fashionpedia():
if "file" not in request.files:
return jsonify({"status":"error","message":"No File Part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"status":"error","message":"No Selected Files"}),400
if file:
try:
data = file.read()
image_bytes = core.object_detection(data,object_detection_yolos_fashionpedia_api_token)
# Parse the JSON string into a Python list of dictionaries
data_list = json.loads(image_bytes)
print(data_list)
scores =[]
labels = []
segmented_pictures=[]
for obj in data_list:
score = obj['score']
label = obj['label']
mask_base64 = obj['box']
# DO APPEND STUFF :D
scores.append(score)
labels.append(label)
segmented_pictures.append(mask_base64)
return jsonify({"status":"sucess","Segmented Image":{"labels":labels,"scores":scores,"segemented_pictures":segmented_pictures}})
except Exception as e:
return jsonify({"status":"error","message":str(e)})
# =================================================================================================
# Models : table_transformer_detection (base Model)
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route("/object_detection/table_transformer_detection",methods=["POST"])
def object_detection_table_transformer_detection():
if "file" not in request.files:
return jsonify({"status":"error","message":"No File Part"}),400
file = request.files["file"]
if file.filename == "":
return jsonify({"status":"error","message":"No Selected Files"}),400
if file:
try:
data = file.read()
image_bytes = core.object_detection(data,object_detection_table_transformer_detection_api_token)
# Parse the JSON string into a Python list of dictionaries
data_list = json.loads(image_bytes)
print(data_list)
scores =[]
labels = []
segmented_pictures=[]
for obj in data_list:
score = obj['score']
label = obj['label']
mask_base64 = obj['box']
# DO APPEND STUFF :D
scores.append(score)
labels.append(label)
segmented_pictures.append(mask_base64)
return jsonify({"status":"sucess","Segmented Image":{"labels":labels,"scores":scores,"segemented_pictures":segmented_pictures}})
except Exception as e:
return jsonify({"status":"error","message":str(e)})
#! ------------------------------------------------------------------------------------------------------
#! # IMAGE TO IMAGE
#! ------------------------------------------------------------------------------------------------------
#? I T N E E D S M O R E O F P R O M P T E N G I N E E R I N G
# =================================================================================================
# Models : Gemini + OPENDALLE (base Model)
# Speciality: Base Models
# prompt : {"file":files}
# function : images merging Version (1)
# =================================================================================================
@app.route("/image_to_image/gemini_opendalle/V1",methods=["POST"])
def image_to_image_gemini_opendalle():
if "file1" not in request.files and "file2" not in request.files:
return jsonify({"status":"error","message":"Your request is incomplete!"}),400
file1 = request.files["file1"]
file2 = request.files["file2"]
if (file1.filename == "" and file2.filename == "") :
return jsonify({"status":"error","message":"No Selected Image(s)"}),400
try:
if file2 and file1:
img1 = file1.read()
img2 = file2. read()
data = {"prompt": "i want you to give me the descripition of how would be the output image if we merged these two pictures toghther,you are not including all details please include them all and describe this image for me including all details as an example (gender . . .) and then MERGE THEM !!", "role": "you are a professional image describer that gives all details about the input image in 500 words always"}
imgs_data = [img1,img2]
# implement gemmini vision
prompt = core.gemini_img2txt(data,imgs_data)
# transform the prompt into an image using gemini
image_bytes = core.text2image(prompt,txt2img_OPENDALLE_api_token)
# base64 Transformation
base64_encoded_image = base64.b64encode(image_bytes).decode('utf-8')
return jsonify({"status": "success", "Image": base64_encoded_image})
except Exception as e:
return jsonify({"status":"error","message":str(e)})
#? I T N E E D S M O R E O F P R O M P T E N G I N E E R I N G
#? I T T A K E S A L O T O F T I M E
# =================================================================================================
# Models : Gemini + OPENDALLE (base Model)
# Speciality: Base Models
# prompt : {"file":files}
# function : images merging Version (2)
# =================================================================================================
@app.route("/image_to_image/gemini_opendalle/V2",methods=["POST"])
def image_to_image_gemini_opendalleV2():
if "file1" not in request.files and "file2" not in request.files and "prompt" not in request.form and "role" not in request.form :
return jsonify({"status":"error","message":"Your request is incomplete!"}),400
file1 = request.files["file1"]
file2 = request.files["file2"]
if (file1.filename == "" and file2.filename == "") :
return jsonify({"status":"error","message":"No Selected Image(s)"}),400
try:
if file2 and file1:
img1 = file1.read()
img2 = file2.read()
data = {"prompt": "describe this image for me including all details as an example (gender . . .)", "role": "you are a professional image describer that gives all details about the input image in 500 words always"}
# implement gemmini vision
prompt1 = core.gemini_img2txt(data,[img1])
prompt2 = core.gemini_img2txt(data,[img2])
# merging prompts
txt_genertation_role = "you are a professional descriptions merging master"
txt_generation_prompt = "i want you to merge these two descriptions and give me the description that would result if we merged these two descriptions"
final_prompt = core.text_generation(txt_genertation_role,txt_generation_prompt)
# transform the prompt into an image using gemini
image_bytes = core.text2image(final_prompt,txt2img_OPENDALLE_api_token)
# base64 Transformation
base64_encoded_image = base64.b64encode(image_bytes).decode('utf-8')
return jsonify({"status": "success", "Image": base64_encoded_image})
except Exception as e:
return jsonify({"status":"error","message":str(e)})
#! ------------------------------------------------------------------------------------------------------
#! #EMOJI TO IMG
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Model : OPENDALLE-v1 + Gemini (Base Models)
# Speciality: Base Model
# Prompt : {"prompt":":Happy_face"}
# function : it generates images based on emotes only
# =================================================================================================
@app.route("/emoji_to_image/OPENDALLE_gemni", methods=["POST"])
def emotji_to_image():
if "prompt" not in request.form :
return jsonify({"status":"error","message":"Your request is incomplete!"}),400
emoji = request.form["prompt"]
try:
prompt = core.text_generation(emoji + " in one word what does emoji represent ?","")
image_generation_prompt = "ultra-realistic,16k,smooth,focus,super resolution,high-quality"
image_bytes = core.text2image(emoji+image_generation_prompt,txt2img_OPENDALLE_api_token)
print(image_bytes)
base64_encoded_image = base64.b64encode(image_bytes).decode("UTF-8")
return jsonify({"status": "success", "image": base64_encoded_image})
except Exception as e:
# Return an error response if an exception occurs
return jsonify({"status": "error", "message": f"Error generating image: {str(e)}"}),500
#! ------------------------------------------------------------------------------------------------------
#! #IMG TO EMOJI
#! ------------------------------------------------------------------------------------------------------
# =================================================================================================
# Model : Gemnin (base model)
# Speciality: Base Model
# prompt : {"file":file}
# =================================================================================================
@app.route('/image_to_emoji/gemini', methods=['POST'])
def image_to_emoji():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
if file:
try:
image = file.read()
data = {"prompt" : "transform this image into emojis, you can include more then a single emoji " , "role":"you should only use emojis no words are allowed"}
result = core.gemini_img2txt(data,[image])
return jsonify({"status": "success", "text": result})
except Exception as e:
return jsonify({"status": "error", "message": f"Error generating emoji: {str(e)}"}),500
# ------------------------------------------------------------------------------------------------------
# # Image ID Adaption
# "API IS NOT WORKING CURRENTLY"
# ------------------------------------------------------------------------------------------------------
def query(payload):
response = requests.post(img_id_api_token, headers=headers, json=payload)