SPACERUNNER99 commited on
Commit
576929d
·
verified ·
1 Parent(s): 6903bca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -78
app.py CHANGED
@@ -1,123 +1,63 @@
1
- from moviepy import VideoFileClip
2
- import gradio as gr
3
  import requests
4
  import os
5
  import time
6
  from gradio_client import Client, handle_file
7
- from pathlib import Path
8
 
9
 
10
- # Store processed videos temporarily
11
- PROCESSED_DIR = "processed_videos"
12
- Path(PROCESSED_DIR).mkdir(exist_ok=True)
13
-
14
  def main(url, parameters, progress=gr.Progress()):
15
  try:
16
  font_type, font_size, font_color, service, target, style, subject = parameters.split(',')
17
  progress(0, desc="پردازش شروع شد")
18
- yield "پردازش شروع شد", gr.DownloadButton.update(visible=False)
19
 
20
  # Transcription Stage
21
  progress(0.35, desc="تبدیل صوت به متن")
22
- yield "تبدیل صوت به متن", gr.DownloadButton.update(visible=False)
23
 
24
  transcribe_client = Client("rayesh/transcribe")
25
  try:
26
- job = transcribe_client.submit(url, api_name="/transcribe")
27
- while not job.done():
28
  time.sleep(0.5)
29
 
30
  results = job.outputs()[0]
 
31
  if len(results) != 3:
32
  raise ValueError(f"Expected 3 outputs, got {len(results)}")
33
-
34
- srt_file, video, mp3_file = results
35
- finally:
36
- transcribe_client.close()
37
-
38
  # Translation Stage
39
  progress(0.55, desc="در حال ترجمه")
40
- yield "در حال ترجمه", gr.DownloadButton.update(visible=False)
41
 
42
  translate_client = Client("rayesh/translate")
43
  try:
44
- job = translate_client.submit(
45
- handle_file(srt_file),
46
- 3000,
47
- api_name="/translate"
48
- )
49
- while not job.done():
50
- time.sleep(0.3)
51
- subtitle_file = job.outputs()[0]
52
- finally:
53
- translate_client.close()
54
 
55
  # Video Processing Stage
56
  progress(0.75, desc="پردازش ویدئو")
57
- yield "درحال پردازش ویدئو", gr.DownloadButton.update(visible=False)
58
 
59
  video_client = Client("SPACERUNNER99/video_edite")
60
  try:
61
- job = video_client.submit(
62
- handle_file(subtitle_file),
63
- {"video": handle_file(video["video"])},
64
- font_color,
65
- font_type,
66
- font_size,
67
- handle_file(mp3_file),
68
- api_name="/video_edit"
69
- )
70
- while not job.done():
71
  time.sleep(0.5)
72
 
73
  output_video = job.outputs()[0]['video']
74
-
75
- # Save to processed directory with unique name
76
- final_path = str(Path(PROCESSED_DIR) / f"output_{int(time.time())}.mp4")
77
- Path(output_video).rename(final_path)
78
-
79
  finally:
80
  video_client.close()
81
 
82
- yield "پردازش کامل شد", gr.DownloadButton.update(value=final_path, visible=True)
83
 
84
  except Exception as e:
85
  raise gr.Error(f"خطا در پردازش: {str(e)}")
86
 
87
- def after_download():
88
- return [gr.DownloadButton.update(visible=False), gr.Button.update(visible=True)]
89
-
90
  with gr.Blocks() as demo:
91
- gr.Markdown("## ویدئو خود را آپلود کنید یا لینک وارد نمایید")
92
  with gr.Column():
93
- progress_output = gr.Textbox(label="وضعیت پردازش", visible=False)
94
- with gr.Row():
95
- video_file_input = gr.Text(label="لینک ویدئو")
96
- upload_btn = gr.UploadButton("فایل ویدئویی", file_types=["video/*"])
97
- parameters = gr.Text(label="تنظیمات زیرنویس (فونت,اندازه,رنگ)")
98
- create_btn = gr.Button("شروع پردازش")
99
- download_btn = gr.DownloadButton("دانلود ویدئو نهایی", visible=False)
100
-
101
- # Handle file upload
102
- upload_btn.upload(
103
- lambda f: [gr.Textbox.update(value=f.name), gr.DownloadButton.update(visible=False)],
104
- upload_btn,
105
- [video_file_input, download_btn]
106
- )
107
-
108
- # Processing handler
109
- create_btn.click(
110
- fn=main,
111
- inputs=[video_file_input, parameters],
112
- outputs=[progress_output, download_btn],
113
- concurrency_limit=4,
114
- )
115
-
116
- # Download completion handler
117
- download_btn.click(
118
- fn=after_download,
119
- inputs=None,
120
- outputs=[download_btn, create_btn]
121
- )
122
-
123
  demo.launch(debug=True)
 
 
 
1
  import requests
2
  import os
3
  import time
4
  from gradio_client import Client, handle_file
 
5
 
6
 
 
 
 
 
7
  def main(url, parameters, progress=gr.Progress()):
8
  try:
9
  font_type, font_size, font_color, service, target, style, subject = parameters.split(',')
10
  progress(0, desc="پردازش شروع شد")
11
+ yield "پردازش شروع شد", None
12
 
13
  # Transcription Stage
14
  progress(0.35, desc="تبدیل صوت به متن")
15
+ yield "تبدیل صوت به متن", None
16
 
17
  transcribe_client = Client("rayesh/transcribe")
18
  try:
 
 
19
  time.sleep(0.5)
20
 
21
  results = job.outputs()[0]
22
+ print(results)
23
  if len(results) != 3:
24
  raise ValueError(f"Expected 3 outputs, got {len(results)}")
 
 
 
 
 
25
  # Translation Stage
26
  progress(0.55, desc="در حال ترجمه")
27
+ yield "در حال ترجمه", None
28
 
29
  translate_client = Client("rayesh/translate")
30
  try:
 
 
 
 
 
 
 
 
 
 
31
 
32
  # Video Processing Stage
33
  progress(0.75, desc="پردازش ویدئو")
34
+ yield "درحال پردازش ویدئو", None
35
 
36
  video_client = Client("SPACERUNNER99/video_edite")
37
  try:
 
 
 
 
 
 
 
 
 
 
38
  time.sleep(0.5)
39
 
40
  output_video = job.outputs()[0]['video']
 
 
 
 
 
41
  finally:
42
  video_client.close()
43
 
44
+ yield "پردازش کامل شد", output_video
45
 
46
  except Exception as e:
47
  raise gr.Error(f"خطا در پردازش: {str(e)}")
48
 
 
 
 
49
  with gr.Blocks() as demo:
50
+ gr.Markdown("Start typing below and then click **Run** to see the progress and final output.")
51
  with gr.Column():
52
+ progress_output = gr.Textbox(label="Progress", visible=False)
53
+ video_file_input = gr.Text(label="Video URL")
54
+ parameters = gr.Text(label="Additional Parameters (for subtitles: color,font)")
55
+ btn = gr.Button("Create")
56
+ video_file_output = gr.Video(label="Result Video")
57
+ btn.click(
58
+ fn=main,
59
+ inputs=[video_file_input, parameters],
60
+ outputs=[progress_output, video_file_output],
61
+ concurrency_limit=4,
62
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  demo.launch(debug=True)