|
import asyncio |
|
import mimetypes |
|
import os |
|
import tempfile |
|
import glob |
|
import fitz |
|
import random |
|
import gradio as gr |
|
from docx import Document |
|
from content_generation import create_content, CONTENT_TYPES |
|
from openai import OpenAI |
|
|
|
|
|
client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY')) |
|
|
|
def create_docx(content, output_path): |
|
""" |
|
Tạo file docx từ nội dung. |
|
""" |
|
doc = Document() |
|
doc.add_paragraph(content) |
|
doc.save(output_path) |
|
|
|
def process_pdf(file_path): |
|
""" |
|
Xử lý file PDF và trích xuất nội dung. |
|
""" |
|
doc = fitz.open(file_path) |
|
text = "" |
|
for page in doc: |
|
text += page.get_text() |
|
return text |
|
|
|
def process_docx(file_path): |
|
""" |
|
Xử lý file DOCX và trích xuất nội dung. |
|
""" |
|
doc = Document(file_path) |
|
text = "" |
|
for para in doc.paragraphs: |
|
text += para.text |
|
return text |
|
|
|
def interface(): |
|
with gr.Blocks() as app: |
|
gr.Markdown("# Ứng dụng Tạo Nội dung và Video") |
|
|
|
with gr.Tab("Tạo Nội dung"): |
|
with gr.Row(): |
|
with gr.Column(): |
|
prompt = gr.Textbox(label="Nhập yêu cầu nội dung") |
|
file_upload = gr.File(label="Tải lên file kèm theo", type="filepath") |
|
content_type = gr.Radio(label="Chọn loại nội dung", |
|
choices=CONTENT_TYPES, |
|
value=None) |
|
content_button = gr.Button("Tạo Nội dung") |
|
|
|
with gr.Column(): |
|
content_output = gr.Textbox(label="Nội dung tạo ra", interactive=True) |
|
confirm_button = gr.Button("Xác nhận nội dung") |
|
download_docx = gr.File(label="Tải xuống file DOCX", interactive=False) |
|
status_message = gr.Label(label="Trạng thái") |
|
|
|
def generate_content(prompt, file, content_type): |
|
try: |
|
status = "Đang xử lý..." |
|
if file and os.path.exists(file): |
|
mime_type, _ = mimetypes.guess_type(file) |
|
if mime_type == "application/pdf": |
|
file_content = process_pdf(file) |
|
prompt = f"{prompt}\n\nDưới đây là nội dung của file tài liệu:\n\n{file_content}" |
|
elif mime_type in ( |
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
|
"application/msword"): |
|
file_content = process_docx(file) |
|
prompt = f"{prompt}\n\nDưới đây là nội dung của file tài liệu:\n\n{file_content}" |
|
else: |
|
raise ValueError("Định dạng file không được hỗ trợ.") |
|
|
|
if not content_type: |
|
raise ValueError("Vui lòng chọn một loại nội dung") |
|
|
|
script_content = create_content(prompt, content_type, "Tiếng Việt") |
|
docx_path = "script.docx" |
|
create_docx(script_content, docx_path) |
|
|
|
status = "Đã tạo nội dung thành công!" |
|
return script_content, docx_path, status |
|
except Exception as e: |
|
status = f"Đã xảy ra lỗi: {str(e)}" |
|
return "", None, status |
|
|
|
async def confirm_content(content): |
|
docx_path = "script.docx" |
|
create_docx(content, docx_path) |
|
|
|
content_button.click(generate_content, |
|
inputs=[prompt, file_upload, content_type], |
|
outputs=[content_output, download_docx, status_message]) |
|
|
|
return app |
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
app = interface() |
|
app.launch(share=True) |