Jelajahi Sumber

Merge branch 'master' of http://git.choozmo.com:3000/choozmo/AI_Anchor_2

huai-sian 3 tahun lalu
induk
melakukan
dda15c9ba8
10 mengubah file dengan 127 tambahan dan 71 penghapusan
  1. TEMPAT SAMPAH
      api/__pycache__/gSlide.cpython-39.pyc
  2. TEMPAT SAMPAH
      api/__pycache__/mailer.cpython-39.pyc
  3. TEMPAT SAMPAH
      api/__pycache__/main.cpython-39.pyc
  4. 14 26
      api/gSlide.py
  5. 100 27
      api/main.py
  6. TEMPAT SAMPAH
      api/mypic.jpg
  7. 13 0
      api/pyppt.py
  8. TEMPAT SAMPAH
      api/test.pptx
  9. 0 18
      api/test.py
  10. TEMPAT SAMPAH
      api/util/__pycache__/models.cpython-39.pyc

TEMPAT SAMPAH
api/__pycache__/gSlide.cpython-39.pyc


TEMPAT SAMPAH
api/__pycache__/mailer.cpython-39.pyc


TEMPAT SAMPAH
api/__pycache__/main.cpython-39.pyc


+ 14 - 26
api/gSlide.py

@@ -1,5 +1,3 @@
-
-
 from __future__ import print_function
 import os.path
 from googleapiclient.discovery import build
@@ -16,6 +14,7 @@ import time
 import os
 import shutil
 from distutils.util import strtobool
+from pptx import Presentation
 
 SCOPES = ['https://www.googleapis.com/auth/presentations.readonly',
           'https://www.googleapis.com/auth/drive.metadata.readonly']
@@ -79,29 +78,18 @@ def parse_slide_url(slide_url,eng):
 
     return slide_content['name'],slide_content['text_content'],slide_content['image_urls']
 
-def parse_slide_url(fileanme,img_upload_folder,img_url,eng):
-    
-    notes_list=[]
-    sub_title_list=[]
-    img_list=[]
-    
+#www.choozmo.com:8168/tmp_pttx/1634388575388378.pptx
+#www.choozmo.com:8168/tmp_img/16344598943505244.jpg
+def parse_pttx_url(fileanme,img_upload_folder,img_url,eng):
     prs = Presentation(fileanme)
+    filename = filename[:filename.find('#')]
+    imgurl = []
     for slide in prs.slides:
-        notes_slide = slide.notes_slide
-        text_frame = notes_slide.notes_text_frame
-        print(text_frame.text)
-        shapes = slide.shapes
-        notes_list.append(text_frame.text)
-        for s in shapes:
-            img_name = str(time.time()).replace('.','')
-            image = s.image
-            image_bytes = image.blob
-            # ---make up a name for the file, e.g. 'image.jpg'---
-            image = Image.open(image_bytes)
-            image= image.convert("RGB")
-            image.save(img_upload_folder+img_name+'.jpg')
-            img_list.append(img_url+img_name+'.jpg')
-            #image_filename = ''
-            #with open(image_filename, 'wb') as f:
-            #    f.write(image_bytes)
-    return filename, notes_list, img_list
+        for shape in slide.shapes:
+            with open('mypic.jpg', 'wb') as f:
+                f.write(shape.image.blob)
+    text = []
+    for page, slide in enumerate(prs.slides):
+        textNote = slide.notes_slide.notes_text_frame.text
+        print(textNote)
+    return filename, imgurl, text

+ 100 - 27
api/main.py

@@ -415,10 +415,10 @@ async def create_upload_file(file: UploadFile = File(...)):
 @app.post("/upload_pttx/")
 async def upload_pttx(file: UploadFile = File(...)):
     try:
-        if "_" in file.filename:
-            return {'msg':{'eng':'symbol"_" is not allowed in file name','zh':'檔案無法使用檔名不能含有"_"符號'}}
+        if "#" in file.filename:
+            return {'msg':{'eng':'symbol"#" is not allowed in file name','zh':'檔案無法使用檔名不能含有"#"符號'}}
         else:
-            pttx_name = file.filename+'_'+str(time.time()).replace('.','')
+            pttx_name = file.filename.replace('.pptx','#')+str(time.time()).replace('.','')+'.pptx'
             with open(pttx_dest+pttx_name, "wb+") as file_object:
                 file_object.write(file.file.read())
             return {"msg": resource_server+pttx_sub_folder+pttx_name}
@@ -426,7 +426,6 @@ async def upload_pttx(file: UploadFile = File(...)):
         logging.error(traceback.format_exc())
         return {'msg':{'eng':'file cant be prossessed','zh':'檔案無法使用'}}
 
-
 @app.post("/make_anchor_video_gSlide")
 async def make_anchor_video_gSlide(req:util.models.gSlide_req,token: str = Depends(oauth2_scheme)):
     if req.url_type == 0:
@@ -434,29 +433,23 @@ async def make_anchor_video_gSlide(req:util.models.gSlide_req,token: str = Depen
     else :
         filename = req.slide_url.replace(resource_server+pttx_sub_folder,resource_folder+pttx_sub_folder)
         name, text_content, image_urls = gSlide.parse_pttx_url(filename,img_upload_folder,resource_server+tmp_img_sub_folder,eng=False)
+    
     if len(image_urls) != len(text_content):
         return {'msg':{'eng':'number of subtitles and images(videos) should be the same','zh':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}}
+    
     for idx in range(len(image_urls)):
         if 'http' not in image_urls[idx]:
             image_urls[idx] = 'http://'+image_urls[idx]
-    if req.multiLang==0:
-        for txt in text_content:
-            if re.search('[a-zA-Z]', txt) !=None:
-                print('語言錯誤')
-                return {'msg':{'eng':'English is not allowed in subtitles','zh':'輸入字串不能包含英文字!'}}
-            if re.search(',', txt) !=None:
-                print('包含非法符號')
-                return {'msg':{'eng':'symbol "," is not allowede','zh':'輸入不能含有","符號'}}
+    
+    mulitLang, hasError = Check_text_content(text_content)
+    if hasError is not False :
+        return hasError
+
+    imgError = Check_image_url(image_urls)
+    if imgError is not False :
+        return imgError
+    
     name_hash = str(time.time()).replace('.','')
-    for imgu in image_urls:
-        try:
-            if get_url_type(imgu) =='video/mp4':
-                r=requests.get(imgu)
-            else:
-                im = Image.open(requests.get(imgu, stream=True).raw)
-                im= im.convert("RGB")
-        except:
-            return {'msg':{'eng':req.imgurl+'cant be proccessed','zh':"無法辨別圖片網址"+req.imgurl}}
     user_id = get_user_id(token)
     proto_req = util.models.request_normal()
     proto_req.text_content = text_content
@@ -508,7 +501,6 @@ async def make_anchor_video_long(req:util.models.request,token: str = Depends(oa
         returnMsg =  {'msg':{'eng':'Processing video requires a few minutes, please wait for notification','zh':'影片處理需要數分鐘,請等待通知'}}
     return returnMsg
 
-
 @app.post("/make_anchor_video")
 async def make_anchor_video(req:util.models.request,token: str = Depends(oauth2_scheme)):
     db_check()
@@ -540,6 +532,43 @@ async def make_anchor_video(req:util.models.request,token: str = Depends(oauth2_
     x = threading.Thread(target=gen_video_queue, args=(name_hash,req.name, req.text_content, req.image_urls,int(req.avatar),req.multiLang,video_id,user_id))
     x.start()
      
+    if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] >= 3:
+        return {'msg':{'eng':'There are many videos have been processing, please wait.','zh':'目前有多部影片處理中,煩請耐心等候'}}
+    else:
+        return {'msg':{'eng':'Processing video requires a few minutes, please wait for notification','zh':'影片處理需要數分鐘,請等待通知'}}
+    return {'msg':'ok'}
+#not auth 
+@app.post("/make_anchor_video_noAuth")
+async def make_anchor_video_noAuth(req:util.models.request):
+    db_check()
+    if len(req.image_urls) != len(req.text_content):
+        return {'msg':{'eng':'number of subtitles and images(videos) should be the same','zh':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}}
+    for idx in range(len(req.image_urls)):
+        if 'http' not in req.image_urls[idx]:
+            req.image_urls[idx] = 'http://'+req.image_urls[idx]
+    if req.multiLang==0:
+        for txt in req.text_content:
+            if re.search('[a-zA-Z]', txt) !=None:
+                print('語言錯誤')
+                return {'msg':{'eng':'English is not allowed in subtitles','zh':'輸入字串不能包含英文字!'}}
+            if re.search(',', txt) !=None:
+                print('包含非法符號')
+                return {'msg':{'eng':'symbol "," is not allowede','zh':'輸入不能含有","符號'}}
+    name_hash = str(time.time()).replace('.','')
+    for imgu in req.image_urls:
+        try:
+            if get_url_type(imgu) =='video/mp4':
+                r=requests.get(imgu)
+            else:
+                im = Image.open(requests.get(imgu, stream=True).raw)
+                im= im.convert("RGB")
+        except:
+            return {'msg':{'eng':req.imgurl+'cant be proccessed','zh':"無法辨別圖片網址"+req.imgurl}}
+    user_id = get_user_id(token)
+    video_id = save_history(req,name_hash,user_id)
+    x = threading.Thread(target=gen_video_queue, args=(name_hash,req.name, req.text_content, req.image_urls,int(req.avatar),req.multiLang,video_id,user_id))
+    x.start()
+     
     if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] >= 3:
         return {'msg':{'eng':'There are many videos have been processing, please wait.','zh':'目前有多部影片處理中,煩請耐心等候'}}
     else:
@@ -654,6 +683,15 @@ async def history_input(token: str = Depends(oauth2_scheme)):
         logs.append({'id':row['id'],'name':row['name'],'avatar':row['avatar'],'text_content':row['text_content'].split(','),'link':row['link'],'image_urls':row['image_urls'].split(',')})
     return logs
 
+@app.post("/history_input_noAuth")
+async def history_input_noAuth():
+    db_check()
+    statement = 'SELECT * FROM history_input ORDER BY timestamp DESC LIMIT 50'
+
+    logs = []
+    for row in db.query(statement):
+        logs.append({'id':row['id'],'name':row['name'],'avatar':row['avatar'],'text_content':row['text_content'].split(','),'link':row['link'],'image_urls':row['image_urls'].split(',')})
+    return logs
 
 
 @AuthJWT.load_config
@@ -763,9 +801,40 @@ def notify_group(msg):
         headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"}
         r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": msg})
 
+def notify_choozmo(msg):
+    #'WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD' is ChoozmoTeam
+    glist = ['WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD']
+    for gid in glist:
+        headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"}
+        r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": msg})
+
+def Check_text_content(text_content):
+    mulitLang = 0
+    status = False
+    for txt in text_content:
+        if re.search('[a-zA-Z]', txt) !=None:
+            mulitLang = 1
+        if re.search(',', txt) !=None:
+            print('包含非法符號')
+            status = {'msg':{'eng':'symbol "," is not allowede','zh':'輸入不能含有","符號'}}
+    return mulitLang, status
+
+def Check_image_url(image_urls):
+    status =False
+    for imgu in image_urls:
+        try:
+            if get_url_type(imgu) =='video/mp4':
+                r=requests.get(imgu)
+            else:
+                im = Image.open(requests.get(imgu, stream=True).raw)
+                im= im.convert("RGB")
+        except:
+            status = {'msg':{'eng':req.imgurl+'cant be proccessed','zh':"無法辨別圖片網址"+req.imgurl}}
+    return status
+    
 
 def gen_video_long_queue(name_hash,name,text_content, image_urls,avatar,multiLang,video_id,user_id):
-    db_check()
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
     if name_hash == 'keepRunning':
         if first(db.query('SELECT COUNT(1) FROM video_queue where video_type="longvideo"'))['COUNT(1)'] == 0:
             return 
@@ -829,9 +898,10 @@ def gen_video_long_queue(name_hash,name,text_content, image_urls,avatar,multiLan
             notify_group('長影片錯誤-測試')
         db['video_queue'].delete(id=top1['id'])
         db.query('UPDATE video_queue_status SET status = 0')
+    db.close()
 
 def gen_video_queue(name_hash,name,text_content, image_urls,avatar,multiLang,video_id,user_id):
-    db_check()
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
     if name_hash == 'keepRunning':
         if first(db.query('SELECT COUNT(1) FROM video_queue where video_type="short"'))['COUNT(1)'] == 0:
             return 
@@ -895,9 +965,10 @@ def gen_video_queue(name_hash,name,text_content, image_urls,avatar,multiLang,vid
             notify_group('影片錯誤')
         db['video_queue'].delete(id=top1['id'])
         db.query('UPDATE video_queue_status SET status = 0')
+    db.close()
 
 def gen_video_queue_eng(name_hash,name,text_content, image_urls,sub_titles,avatar,video_id):
-    db_check()
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
     if name_hash == 'keepRunning':
         if first(db.query('SELECT COUNT(1) FROM video_queue where video_type="eng"'))['COUNT(1)'] == 0:
             return 
@@ -941,6 +1012,7 @@ def gen_video_queue_eng(name_hash,name,text_content, image_urls,sub_titles,avata
             notify_group('影片錯誤')
         db['video_queue'].delete(id=top1['id'])
         db.query('UPDATE video_queue_status SET status = 0')
+    db.close()
 
 def gen_avatar(name_hash, imgurl):
     db_check()
@@ -1029,7 +1101,7 @@ def call_speech(text,speaker):
     fw.close()
     shutil.copy(text+'.wav',speech_dest+text+'.wav')
     os.remove(text+'.wav')
-    notify_group("speech at www.choozmo.com:8168/speech/"+text+".wav")
+    notify_choozmo("speech at www.choozmo.com:8168/speech/"+text+".wav")
 
 
 def verify_jwt_token(token):
@@ -1042,7 +1114,8 @@ def verify_jwt_token(token):
         return 'please login again'
 def db_check():
     global db
-    db.close()
+    if db!=None:
+        db.close()
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
 
 

TEMPAT SAMPAH
api/mypic.jpg


+ 13 - 0
api/pyppt.py

@@ -0,0 +1,13 @@
+from pptx import Presentation
+
+prs = Presentation('test.pptx')
+
+
+for slide in prs.slides:
+    for shape in slide.shapes:
+        with open('mypic.jpg', 'wb') as f:
+            f.write(shape.image.blob)
+
+for page, slide in enumerate(prs.slides):
+    textNote = slide.notes_slide.notes_text_frame.text
+    print(textNote)

TEMPAT SAMPAH
api/test.pptx


+ 0 - 18
api/test.py

@@ -1,18 +0,0 @@
-import util.user
-import dataset
-import models
-from passlib.context import CryptContext
-import time
-import mailer
-import first
-
-def prof():
-    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
-    user_id = 36
-
-
-    user_obj = next(iter(db.query('SELECT * FROM users where id ="'+str(user_id)+'"')))
-    user_obj['email'] = 'ming'
-    db['users'].update(dict(user_obj), ['id'])
-
-prof()

TEMPAT SAMPAH
api/util/__pycache__/models.cpython-39.pyc