eternalBlissard commited on
Commit
fd57e3d
·
verified ·
1 Parent(s): fc8aafc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import os
4
+ import ffmpeg
5
+ # import pymedia.audio.acodec as acodec
6
+ # import pymedia.muxer as muxer
7
+ import random
8
+ import string
9
+ import spaces
10
+ from openai import OpenAI
11
+ import os
12
+ import re
13
+ from math import floor
14
+
15
+
16
+ ACCESS_TOKEN = os.getenv("HF_TOKEN")
17
+
18
+ client = OpenAI(
19
+ base_url="https://api-inference.huggingface.co/v1/",
20
+ api_key=ACCESS_TOKEN,
21
+ )
22
+
23
+
24
+ def random_name_generator():
25
+ length = random.randint(10, 15) # Random length between 10 and 15
26
+ characters = string.ascii_letters + string.digits # All alphanumeric characters
27
+ random_name = ''.join(random.choice(characters) for _ in range(length))
28
+ return random_name
29
+
30
+ # Example usage:
31
+ # print(random_name_generator())
32
+
33
+ @spaces.GPU()
34
+ def outputProducer(inputVideo):
35
+ print(inputVideo)
36
+ input_file = ffmpeg.input(inputVideo)
37
+ name_random = random_name_generator()
38
+ input_file.output('audio'+name_random+'.mp3', acodec='mp3').run()
39
+ command2 = ["whisper",'./audio'+name_random+'.mp3']
40
+ try:
41
+ retVal = subprocess.check_output(command2)
42
+ except:
43
+ retVal = subprocess.check_output("ls")
44
+ subprocess.run(['rm', 'audio'+name_random+'.mp3'], check=True)
45
+ return retVal
46
+
47
+ def subtitle_it(subtitle_str):
48
+ # Regular expression to extract time and text
49
+ pattern = re.compile(
50
+ r'\[(\d{2}):(\d{2})\.(\d{3})\s*-->\s*(\d{2}):(\d{2})\.(\d{3})\]\s*(.*)'
51
+ )
52
+ # List to hold subtitle entries as tuples: (start_time, end_time, text)
53
+ subtitles = []
54
+ subtitle_str = subtitle_str.decode('utf-8') # or replace 'utf-8' with the appropriate encoding if needed
55
+ max_second = 0 # To determine the size of the list L
56
+
57
+ sub_string = ""
58
+ # Parse each line
59
+ for line in subtitle_str.strip().split('\n'):
60
+ match = pattern.match(line)
61
+ if match:
62
+ (
63
+ start_min, start_sec, start_ms,
64
+ end_min, end_sec, end_ms,
65
+ text
66
+ ) = match.groups()
67
+
68
+ # Convert start and end times to total seconds
69
+ sub_string+=text
70
+
71
+ # Update maximum second
72
+ else:
73
+ print(f"Line didn't match pattern: {line}")
74
+ return sub_string
75
+ # Initialize list L with empty strings
76
+
77
+ def respond(
78
+ message,
79
+ history: list[tuple[str, str]],
80
+ system_message,
81
+ max_tokens,
82
+ temperature,
83
+ top_p,
84
+ ):
85
+ messages = [{"role": "system", "content": system_message}]
86
+
87
+ for val in history:
88
+ if val[0]:
89
+ messages.append({"role": "user", "content": val[0]})
90
+ if val[1]:
91
+ messages.append({"role": "assistant", "content": val[1]})
92
+
93
+ messages.append({"role": "user", "content": message})
94
+
95
+ response = ""
96
+
97
+ for message in client.chat.completions.create(
98
+ model="Qwen/Qwen2.5-72B-Instruct",
99
+ max_tokens=max_tokens,
100
+ stream=True,
101
+ temperature=temperature,
102
+ top_p=top_p,
103
+ messages=messages,
104
+ ):
105
+ token = message.choices[0].delta.content
106
+
107
+ response += token
108
+ yield response
109
+
110
+ chatbot = gr.Chatbot(height=600)
111
+
112
+ demo = gr.ChatInterface(
113
+ respond,
114
+ additional_inputs=[
115
+ gr.Video(value=None, label="System message"),
116
+ gr.Slider(minimum=1, maximum=4098, value=1024, step=1, label="Max new tokens"),
117
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
118
+ gr.Slider(
119
+ minimum=0.1,
120
+ maximum=1.0,
121
+ value=0.95,
122
+ step=0.05,
123
+ label="Top-P",
124
+ ),
125
+
126
+ ],
127
+ fill_height=True,
128
+ chatbot=chatbot
129
+ )
130
+ if __name__ == "__main__":
131
+ demo.launch()
132
+
133
+