Spaces:
Sleeping
Sleeping
prithivMLmods
commited on
Commit
•
c6544ea
1
Parent(s):
a18d4ca
Update app.py
Browse files
app.py
CHANGED
@@ -1,138 +1,99 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
from
|
5 |
-
import
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
css =
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
13 |
}
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
text_height = sum([font.getbbox(line)[3] - font.getbbox(line)[1] for line in wrapped_lines])
|
37 |
-
|
38 |
-
if text_height <= image_height - 40:
|
39 |
-
break
|
40 |
-
font_size -= 1
|
41 |
-
font = ImageFont.truetype(font_path, font_size)
|
42 |
-
|
43 |
-
# Generate frames for the typing effect
|
44 |
-
for char in code_text:
|
45 |
-
current_text += char
|
46 |
-
|
47 |
-
if format_choice == "Paragraph":
|
48 |
-
wrapped_lines = textwrap.wrap(current_text, width=max_width // font.getlength(' '))
|
49 |
-
else: # Programming
|
50 |
-
wrapped_lines = current_text.splitlines()
|
51 |
-
|
52 |
-
# Copy the background image for each frame
|
53 |
-
image = background.copy()
|
54 |
-
draw = ImageDraw.Draw(image)
|
55 |
-
|
56 |
-
y_position = 20
|
57 |
-
for line in wrapped_lines:
|
58 |
-
draw.text((20, y_position), line, font=font, fill=text_color)
|
59 |
-
line_height = font.getbbox(line)[3] - font.getbbox(line)[1]
|
60 |
-
y_position += line_height * line_spacing
|
61 |
-
|
62 |
-
# Convert to numpy array for OpenCV
|
63 |
-
frame = np.array(image)
|
64 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
65 |
-
video_frames.append(frame)
|
66 |
-
|
67 |
-
# Create a video writer
|
68 |
-
video_filename = "typed_code_video.mp4"
|
69 |
-
out = cv2.VideoWriter(video_filename, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (image_width, image_height))
|
70 |
|
71 |
-
for
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
video_filename = "speed_adjusted_video.mp4"
|
87 |
-
|
88 |
-
# Add sound if a sound choice is selected
|
89 |
-
if sound_choice and sound_choice != "No Sound":
|
90 |
-
video = mp.VideoFileClip(video_filename)
|
91 |
-
audio = mp.AudioFileClip(f"type-sounds/{sound_choice}")
|
92 |
-
|
93 |
-
# Loop the audio to match the duration of the video
|
94 |
-
audio = audio.fx(mp.afx.audio_loop, duration=video.duration)
|
95 |
-
video = video.set_audio(audio)
|
96 |
-
video.write_videofile("typed_code_video_with_sound.mp4", codec="libx264")
|
97 |
-
video_filename = "typed_code_video_with_sound.mp4"
|
98 |
-
|
99 |
-
# Add custom audio if provided
|
100 |
-
if custom_audio:
|
101 |
-
video = mp.VideoFileClip(video_filename)
|
102 |
-
audio = mp.AudioFileClip(custom_audio)
|
103 |
-
|
104 |
-
# Loop the custom audio to match the duration of the video
|
105 |
-
audio = audio.fx(mp.afx.audio_loop, duration=video.duration)
|
106 |
-
video = video.set_audio(audio)
|
107 |
-
video.write_videofile("typed_code_video_with_custom_audio.mp4", codec="libx264")
|
108 |
-
video_filename = "typed_code_video_with_custom_audio.mp4"
|
109 |
-
|
110 |
-
# Apply video quality enhancement if enabled
|
111 |
-
if enhance_quality:
|
112 |
-
video = mp.VideoFileClip(video_filename)
|
113 |
-
video = video.fx(vfx.resize, height=720) # Resize video to enhance quality
|
114 |
-
video = video.fx(vfx.colorx, 1.2) # Increase contrast
|
115 |
-
video.write_videofile("enhanced_" + video_filename, codec="libx264")
|
116 |
-
video_filename = "enhanced_" + video_filename
|
117 |
-
|
118 |
-
return video_filename
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
)
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
)
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
font_choice = gr.Dropdown(
|
137 |
choices=[
|
138 |
"DejaVuMathTeXGyre.ttf",
|
@@ -152,7 +113,7 @@ font_choice = gr.Dropdown(
|
|
152 |
"COOPBL.TTF",
|
153 |
"Rockwell-Bold.ttf",
|
154 |
"Candara Light.TTF",
|
155 |
-
"Carlito-Regular.ttf
|
156 |
"Castellar.ttf",
|
157 |
"Courier New.ttf",
|
158 |
"LSANS.TTF",
|
@@ -167,99 +128,48 @@ font_choice = gr.Dropdown(
|
|
167 |
"youyuan.TTF",
|
168 |
"TfPonetoneExpanded-7BJZA.ttf",
|
169 |
],
|
170 |
-
value="
|
171 |
-
label="
|
172 |
)
|
173 |
|
|
|
174 |
font_size = gr.Dropdown(
|
175 |
-
choices=["16", "18", "20", "22", "24"],
|
176 |
value="18",
|
177 |
label="Font Size"
|
178 |
)
|
179 |
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
height_choice = gr.Dropdown(
|
187 |
-
choices=["400", "720", "1080", "1440", "2160"],
|
188 |
-
value="400",
|
189 |
-
label="Video Height"
|
190 |
-
)
|
191 |
-
|
192 |
-
sound_choice = gr.Dropdown(
|
193 |
-
choices=["No Sound",
|
194 |
-
"Mediumspeed Typing.mp3",
|
195 |
-
"Speed Typing.mp3",
|
196 |
-
"Bass Typing.mp3",
|
197 |
-
"Bay Typing.mp3",
|
198 |
-
"Crack Typing.mp3",
|
199 |
-
"Deep Sence Typing.mp3",
|
200 |
-
"Flacking Typing.mp3",
|
201 |
-
"Flaw Typing.mp3",
|
202 |
-
"Focused Typing.mp3",
|
203 |
-
"K55 Typing.mp3",
|
204 |
-
"Laptop Typing.mp3",
|
205 |
-
"NDC Typing.mp3",
|
206 |
-
"RedMECH Typing.mp3",
|
207 |
-
"Smooth Typing.mp3",
|
208 |
-
"Stop Tpying.mp3",
|
209 |
-
],
|
210 |
-
value="No Sound",
|
211 |
-
label="Typing Sound"
|
212 |
-
)
|
213 |
-
custom_audio = gr.File(
|
214 |
-
label="Upload Custom Audio SFX🔊",
|
215 |
-
type="filepath"
|
216 |
-
)
|
217 |
-
|
218 |
-
background_color = gr.Dropdown(
|
219 |
-
choices=["black", "white", "darkblue", "orange", "green"],
|
220 |
-
value="black",
|
221 |
-
label="Background Color"
|
222 |
-
)
|
223 |
-
|
224 |
-
text_color = gr.Dropdown(
|
225 |
-
choices=["black", "white", "darkblue", "orange", "green"],
|
226 |
-
value="white",
|
227 |
-
label="Text Color"
|
228 |
-
)
|
229 |
-
|
230 |
-
enhance_quality = gr.Checkbox(
|
231 |
-
label="Enhance Video Quality"
|
232 |
-
)
|
233 |
-
|
234 |
-
video_speed = gr.Dropdown(
|
235 |
-
choices=["1x", "1.25x", "1.5x", "1.75x", "2x"],
|
236 |
-
value="1x",
|
237 |
-
label="Video Speed"
|
238 |
)
|
239 |
|
240 |
-
|
241 |
-
|
|
|
242 |
inputs=[
|
243 |
-
gr.Textbox(
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
font_choice,
|
249 |
-
font_size,
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
|
|
|
|
|
|
256 |
],
|
257 |
-
|
258 |
-
outputs=gr.Video(label="Typing Video"),
|
259 |
-
title="Type Bytes🐧",
|
260 |
css=css,
|
261 |
-
|
|
|
262 |
)
|
263 |
|
264 |
-
|
265 |
-
iface.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
+
import os
|
4 |
+
from fpdf import FPDF
|
5 |
+
import docx
|
6 |
+
|
7 |
+
# Fetching the access token from environment variables
|
8 |
+
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
9 |
+
|
10 |
+
# Initialize the OpenAI client
|
11 |
+
client = OpenAI(
|
12 |
+
base_url="https://api-inference.huggingface.co/v1/",
|
13 |
+
api_key=ACCESS_TOKEN,
|
14 |
+
)
|
15 |
|
16 |
+
css = '''
|
17 |
+
.gradio-container{max-width: 1000px !important}
|
18 |
+
h1{text-align:center}
|
19 |
+
footer {
|
20 |
+
visibility: hidden
|
21 |
}
|
22 |
+
'''
|
23 |
+
|
24 |
+
# Function to format and respond to the user's message using the Llama model
|
25 |
+
def respond(
|
26 |
+
message,
|
27 |
+
history: list[tuple[str, str]],
|
28 |
+
system_message,
|
29 |
+
max_tokens,
|
30 |
+
temperature,
|
31 |
+
top_p,
|
32 |
+
):
|
33 |
+
messages = [{"role": "system", "content": system_message}]
|
34 |
+
|
35 |
+
for val in history:
|
36 |
+
if val[0]:
|
37 |
+
messages.append({"role": "user", "content": val[0]})
|
38 |
+
if val[1]:
|
39 |
+
messages.append({"role": "assistant", "content": val[1]})
|
40 |
+
|
41 |
+
messages.append({"role": "user", "content": message})
|
42 |
+
|
43 |
+
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
for message in client.chat.completions.create(
|
46 |
+
model="meta-llama/Meta-Llama-3.1-70B-Instruct",
|
47 |
+
max_tokens=max_tokens,
|
48 |
+
stream=True,
|
49 |
+
temperature=temperature,
|
50 |
+
top_p=top_p,
|
51 |
+
messages=messages,
|
52 |
+
):
|
53 |
+
token = message.choices[0].delta.content
|
54 |
+
response += token
|
55 |
+
yield response
|
56 |
+
|
57 |
+
# Save the generated content to a file with the specified font, font size, and line spacing
|
58 |
+
def save_file(content, filename, file_format, font_name, font_size, line_spacing):
|
59 |
+
font_path = f"font/{font_name}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
if file_format == "pdf":
|
62 |
+
pdf = FPDF()
|
63 |
+
pdf.add_page()
|
64 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
65 |
+
pdf.add_font("CustomFont", "", font_path, uni=True)
|
66 |
+
pdf.set_font("CustomFont", size=int(font_size))
|
67 |
+
for line in content.split("\n"):
|
68 |
+
pdf.multi_cell(0, 10 * float(line_spacing), line)
|
69 |
+
pdf.output(f"{filename}.pdf")
|
70 |
+
return f"{filename}.pdf"
|
71 |
+
elif file_format == "docx":
|
72 |
+
doc = docx.Document()
|
73 |
+
paragraph = doc.add_paragraph(content)
|
74 |
+
run = paragraph.runs[0]
|
75 |
+
run.font.name = font_name.split(".")[0] # Use the font name without extension
|
76 |
+
run.font.size = docx.shared.Pt(int(font_size)) # Set the font size
|
77 |
+
paragraph_format = paragraph.paragraph_format
|
78 |
+
paragraph_format.line_spacing = float(line_spacing) # Set the line spacing
|
79 |
+
doc.save(f"{filename}.docx")
|
80 |
+
return f"{filename}.docx"
|
81 |
+
elif file_format == "txt":
|
82 |
+
with open(f"{filename}.txt", "w") as f:
|
83 |
+
f.write(content)
|
84 |
+
return f"{filename}.txt"
|
85 |
+
else:
|
86 |
+
raise ValueError("Unsupported file format")
|
87 |
+
|
88 |
+
# Combine respond and save file functions
|
89 |
+
def respond_and_save(message, history, system_message, filename="output", file_format="pdf", font_name="arial.ttf", font_size="18", line_spacing="1.5", max_tokens=512, temperature=0.7, top_p=0.95):
|
90 |
+
generated_text = ""
|
91 |
+
for output in respond(message, history, system_message, max_tokens, temperature, top_p):
|
92 |
+
generated_text = output
|
93 |
+
saved_file = save_file(generated_text, filename, file_format, font_name, font_size, line_spacing)
|
94 |
+
return generated_text, history + [(message, generated_text)], saved_file
|
95 |
+
|
96 |
+
# Create the font dropdown
|
97 |
font_choice = gr.Dropdown(
|
98 |
choices=[
|
99 |
"DejaVuMathTeXGyre.ttf",
|
|
|
113 |
"COOPBL.TTF",
|
114 |
"Rockwell-Bold.ttf",
|
115 |
"Candara Light.TTF",
|
116 |
+
"Carlito-Regular.ttf",
|
117 |
"Castellar.ttf",
|
118 |
"Courier New.ttf",
|
119 |
"LSANS.TTF",
|
|
|
128 |
"youyuan.TTF",
|
129 |
"TfPonetoneExpanded-7BJZA.ttf",
|
130 |
],
|
131 |
+
value="arial.ttf",
|
132 |
+
label="Font Style"
|
133 |
)
|
134 |
|
135 |
+
# Create the font size dropdown
|
136 |
font_size = gr.Dropdown(
|
137 |
+
choices=["12", "14", "16", "18", "20", "22", "24"],
|
138 |
value="18",
|
139 |
label="Font Size"
|
140 |
)
|
141 |
|
142 |
+
# Create the line spacing dropdown
|
143 |
+
line_spacing = gr.Dropdown(
|
144 |
+
choices=[1.0, 1.15, 1.5, 2.0, 2.5, 3.0],
|
145 |
+
value=1.5,
|
146 |
+
label="Line Spacing"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
)
|
148 |
|
149 |
+
# Create Gradio interface
|
150 |
+
demo = gr.Interface(
|
151 |
+
fn=respond_and_save,
|
152 |
inputs=[
|
153 |
+
gr.Textbox(placeholder="Type your message here...", label="Chatbot", lines=5),
|
154 |
+
gr.State(value=[]),
|
155 |
+
gr.Textbox(placeholder="System message", label="System message", value="", visible=False),
|
156 |
+
gr.Textbox(placeholder="Filename (default: output)", label="Filename", value="output"),
|
157 |
+
gr.Radio(["pdf", "docx", "txt"], label="File Format", value="pdf"),
|
158 |
font_choice,
|
159 |
+
font_size, # Add the font size dropdown to the interface
|
160 |
+
line_spacing, # Add the line spacing dropdown to the interface
|
161 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
162 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature", visible=False),
|
163 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P", visible=False),
|
164 |
+
],
|
165 |
+
outputs=[
|
166 |
+
gr.Textbox(label="Generated Text", lines=5),
|
167 |
+
gr.State(value=[]),
|
168 |
+
gr.File(label="Download File")
|
169 |
],
|
|
|
|
|
|
|
170 |
css=css,
|
171 |
+
title="GRABDOC PRO",
|
172 |
+
theme="bethecloud/storj_theme"
|
173 |
)
|
174 |
|
175 |
+
demo.queue().launch(show_api=False)
|
|