SPACERUNNER99 commited on
Commit
3c163a9
·
verified ·
1 Parent(s): 576929d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -13
app.py CHANGED
@@ -1,12 +1,14 @@
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
 
@@ -16,18 +18,31 @@ def main(url, parameters, progress=gr.Progress()):
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="پردازش ویدئو")
@@ -35,29 +50,52 @@ def main(url, parameters, progress=gr.Progress()):
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)
 
 
1
  import requests
2
  import os
3
  import time
4
+ import tempfile
5
  from gradio_client import Client, handle_file
6
+ import gradio as gr
7
 
8
  def main(url, parameters, progress=gr.Progress()):
9
  try:
10
  font_type, font_size, font_color, service, target, style, subject = parameters.split(',')
11
+
12
  progress(0, desc="پردازش شروع شد")
13
  yield "پردازش شروع شد", None
14
 
 
18
 
19
  transcribe_client = Client("rayesh/transcribe")
20
  try:
21
+ job = transcribe_client.submit(url, api_name="/predict")
22
+ while not job.done():
23
  time.sleep(0.5)
24
+ progress(0.35 + job.progress*0.2, desc="تبدیل صوت به متن")
25
+
26
  results = job.outputs()[0]
 
27
  if len(results) != 3:
28
  raise ValueError(f"Expected 3 outputs, got {len(results)}")
29
+ finally:
30
+ transcribe_client.close()
31
+
32
  # Translation Stage
33
  progress(0.55, desc="در حال ترجمه")
34
  yield "در حال ترجمه", None
35
 
36
  translate_client = Client("rayesh/translate")
37
  try:
38
+ job = translate_client.submit(results[0], target, api_name="/predict")
39
+ while not job.done():
40
+ time.sleep(0.5)
41
+ progress(0.55 + job.progress*0.2, desc="در حال ترجمه")
42
+
43
+ translated_text = job.outputs()[0]
44
+ finally:
45
+ translate_client.close()
46
 
47
  # Video Processing Stage
48
  progress(0.75, desc="پردازش ویدئو")
 
50
 
51
  video_client = Client("SPACERUNNER99/video_edite")
52
  try:
53
+ job = video_client.submit(
54
+ url,
55
+ translated_text,
56
+ font_type,
57
+ font_size,
58
+ font_color,
59
+ api_name="/predict"
60
+ )
61
+ while not job.done():
62
  time.sleep(0.5)
63
+ progress(0.75 + job.progress*0.25, desc="پردازش ویدئو")
64
+
65
  output_video = job.outputs()[0]['video']
66
+
67
+ # Create temporary file
68
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_file:
69
+ # Download the video content
70
+ response = requests.get(output_video)
71
+ tmp_file.write(response.content)
72
+ tmp_file_path = tmp_file.name
73
+
74
+ # Create downloadable URL
75
+ base_url = os.environ.get('GRADIO_SERVER_NAME', 'http://localhost:7860')
76
+ download_url = f"{base_url}/file/{tmp_file_path}"
77
+
78
  finally:
79
  video_client.close()
80
 
81
+ yield "پردازش کامل شد", download_url
82
 
83
  except Exception as e:
84
  raise gr.Error(f"خطا در پردازش: {str(e)}")
85
 
86
  with gr.Blocks() as demo:
87
+ gr.Markdown("ویدئو خود را آپلود کنید و پارامترها را تنظیم نمایید")
88
  with gr.Column():
89
+ progress_output = gr.Textbox(label="وضعیت پردازش", visible=False)
90
+ video_file_input = gr.Text(label="لینک ویدئو")
91
+ parameters = gr.Text(label="پارامترهای زیرنویس (قلم، اندازه، رنگ)")
92
+ btn = gr.Button("ایجاد ویدئو جدید")
93
+ download_link = gr.HTML(label="لینک دانلود")
94
  btn.click(
95
  fn=main,
96
  inputs=[video_file_input, parameters],
97
+ outputs=[progress_output, download_link],
98
  concurrency_limit=4,
99
  )
100
+
101
+ demo.launch(debug=True)