process_miniapp / app.py
SPACERUNNER99's picture
Update app.py
3c163a9 verified
raw
history blame
3.76 kB
import requests
import os
import time
import tempfile
from gradio_client import Client, handle_file
import gradio as gr
def main(url, parameters, progress=gr.Progress()):
try:
font_type, font_size, font_color, service, target, style, subject = parameters.split(',')
progress(0, desc="پردازش شروع شد")
yield "پردازش شروع شد", None
# Transcription Stage
progress(0.35, desc="تبدیل صوت به متن")
yield "تبدیل صوت به متن", None
transcribe_client = Client("rayesh/transcribe")
try:
job = transcribe_client.submit(url, api_name="/predict")
while not job.done():
time.sleep(0.5)
progress(0.35 + job.progress*0.2, desc="تبدیل صوت به متن")
results = job.outputs()[0]
if len(results) != 3:
raise ValueError(f"Expected 3 outputs, got {len(results)}")
finally:
transcribe_client.close()
# Translation Stage
progress(0.55, desc="در حال ترجمه")
yield "در حال ترجمه", None
translate_client = Client("rayesh/translate")
try:
job = translate_client.submit(results[0], target, api_name="/predict")
while not job.done():
time.sleep(0.5)
progress(0.55 + job.progress*0.2, desc="در حال ترجمه")
translated_text = job.outputs()[0]
finally:
translate_client.close()
# Video Processing Stage
progress(0.75, desc="پردازش ویدئو")
yield "درحال پردازش ویدئو", None
video_client = Client("SPACERUNNER99/video_edite")
try:
job = video_client.submit(
url,
translated_text,
font_type,
font_size,
font_color,
api_name="/predict"
)
while not job.done():
time.sleep(0.5)
progress(0.75 + job.progress*0.25, desc="پردازش ویدئو")
output_video = job.outputs()[0]['video']
# Create temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_file:
# Download the video content
response = requests.get(output_video)
tmp_file.write(response.content)
tmp_file_path = tmp_file.name
# Create downloadable URL
base_url = os.environ.get('GRADIO_SERVER_NAME', 'http://localhost:7860')
download_url = f"{base_url}/file/{tmp_file_path}"
finally:
video_client.close()
yield "پردازش کامل شد", download_url
except Exception as e:
raise gr.Error(f"خطا در پردازش: {str(e)}")
with gr.Blocks() as demo:
gr.Markdown("ویدئو خود را آپلود کنید و پارامترها را تنظیم نمایید")
with gr.Column():
progress_output = gr.Textbox(label="وضعیت پردازش", visible=False)
video_file_input = gr.Text(label="لینک ویدئو")
parameters = gr.Text(label="پارامترهای زیرنویس (قلم، اندازه، رنگ)")
btn = gr.Button("ایجاد ویدئو جدید")
download_link = gr.HTML(label="لینک دانلود")
btn.click(
fn=main,
inputs=[video_file_input, parameters],
outputs=[progress_output, download_link],
concurrency_limit=4,
)
demo.launch(debug=True)