TDN-M commited on
Commit
4fcacf9
·
verified ·
1 Parent(s): 7283e13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py CHANGED
@@ -1,3 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def interface():
2
  with gr.Blocks() as app:
3
  gr.Markdown("# Ứng dụng Tạo Nội dung và Video")
 
1
+ import asyncio
2
+ import mimetypes
3
+ import os
4
+ import tempfile
5
+ import glob
6
+ import fitz # PyMuPDF
7
+ import random
8
+ import gradio as gr
9
+ from docx import Document
10
+ from audio_processing import async_text_to_speech, text_to_speech
11
+ from content_generation import create_content, CONTENT_TYPES
12
+ from video_processing import create_video_func
13
+ from moviepy.editor import AudioFileClip, VideoFileClip, CompositeAudioClip
14
+ from utils import (combine_videos, get_pexels_video, get_bgm_file, download_video)
15
+ from video_processing import create_video
16
+ from content_generation import create_content, CONTENT_TYPES
17
+ from openai import OpenAI
18
+
19
+ # Khởi tạo client OpenAI với API key từ biến môi trường
20
+ client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
21
+
22
+ def create_docx(content, output_path):
23
+ """
24
+ Tạo file docx từ nội dung.
25
+ """
26
+ doc = Document()
27
+ doc.add_paragraph(content)
28
+ doc.save(output_path)
29
+
30
+ def process_pdf(file_path):
31
+ """
32
+ Xử lý file PDF và trích xuất nội dung.
33
+ """
34
+ doc = fitz.open(file_path)
35
+ text = ""
36
+ for page in doc:
37
+ text += page.get_text()
38
+ return text
39
+
40
+ def process_docx(file_path):
41
+ """
42
+ Xử lý file DOCX và trích xuất nội dung.
43
+ """
44
+ doc = Document(file_path)
45
+ text = ""
46
+ for para in doc.paragraphs:
47
+ text += para.text
48
+ return text
49
+
50
+ def get_bgm_file_list():
51
+ """
52
+ Trả về danh sách các tệp nhạc nền.
53
+ """
54
+ # Giả sử bạn có một thư mục chứa các tệp nhạc nền
55
+ song_dir = "/data/bg-music"
56
+ return [os.path.basename(file) for file in glob.glob(os.path.join(song_dir, "*.mp3"))]
57
+
58
+ def extract_key_contents(script, num_contents=10):
59
+ """
60
+ Trích xuất các ý chính từ script.
61
+ """
62
+ try:
63
+ response = client.chat.completions.create(
64
+ model="gpt-3.5-turbo",
65
+ messages=[
66
+ {"role": "system", "content": f"Bạn là một chuyên gia phân tích nội dung. Hãy trích xuất chính xác {num_contents} ý chính quan trọng nhất từ đoạn văn sau, mỗi ý không quá 20 từ."},
67
+ {"role": "user", "content": script}
68
+ ]
69
+ )
70
+ # Sửa lỗi truy cập đối tượng không thể subscript
71
+ key_contents = response.choices[0].message.content.split('\n')
72
+ return key_contents[:num_contents]
73
+ except Exception as e:
74
+ print(f"Lỗi khi trích xuất nội dung: {str(e)}")
75
+ return []
76
+
77
  def interface():
78
  with gr.Blocks() as app:
79
  gr.Markdown("# Ứng dụng Tạo Nội dung và Video")