Spaces:
Runtime error
Runtime error
Naphat Sornwichai commited on
Commit Β·
3b1a061
1
Parent(s): 33336b0
update major files
Browse files- app.py +33 -19
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -9,20 +9,25 @@ import time
|
|
| 9 |
import uuid
|
| 10 |
import socket
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
try:
|
| 14 |
addr = socket.gethostbyname('www.youtube.com')
|
| 15 |
-
print(f"---
|
|
|
|
| 16 |
except socket.gaierror as e:
|
| 17 |
-
print(f"---
|
| 18 |
-
print("--- PYTHON NETWORK TEST COMPLETE ---")
|
| 19 |
|
| 20 |
print("Initializing transcription model (faster-whisper)...")
|
| 21 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
compute_type = "float16" if device == "cuda" else "int8"
|
| 23 |
-
model_size = "large-v3
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def download_youtube_audio(url: str) -> str:
|
| 28 |
unique_id = uuid.uuid4()
|
|
@@ -52,7 +57,8 @@ def transcribe_and_summarize(audio_file: str, youtube_url: str):
|
|
| 52 |
|
| 53 |
api_key = os.getenv('TYPHOON_API')
|
| 54 |
if not api_key:
|
| 55 |
-
|
|
|
|
| 56 |
return
|
| 57 |
|
| 58 |
if audio_file is None and not youtube_url:
|
|
@@ -112,6 +118,7 @@ The JSON object must have the following structure:
|
|
| 112 |
key_takeaway = data.get("key_takeaway", "")
|
| 113 |
main_ideas = data.get("main_ideas", [])
|
| 114 |
conclusion = data.get("conclusion", "")
|
|
|
|
| 115 |
summary_markdown = f"# {title}\n\n<p>{key_takeaway}</p>\n\n## Key Ideas\n\n<ul>"
|
| 116 |
for idea in main_ideas:
|
| 117 |
summary_markdown += f"<li>{idea}</li>"
|
|
@@ -128,7 +135,7 @@ def update_video_preview(url):
|
|
| 128 |
return gr.update(value=None, visible=False)
|
| 129 |
video_id = None
|
| 130 |
try:
|
| 131 |
-
if "youtube.com/shorts/" in url:
|
| 132 |
video_id = url.split("/shorts/")[1].split("?")[0]
|
| 133 |
elif "watch?v=" in url:
|
| 134 |
video_id = url.split("watch?v=")[1].split("&")[0]
|
|
@@ -136,14 +143,15 @@ def update_video_preview(url):
|
|
| 136 |
video_id = url.split("youtu.be/")[1].split("?")[0]
|
| 137 |
except IndexError:
|
| 138 |
pass
|
|
|
|
| 139 |
if video_id:
|
| 140 |
-
embed_url = f"https://www.youtube.com/embed/{video_id}"
|
| 141 |
iframe_html = f'<iframe width="100%" height="315" src="{embed_url}" frameborder="0" allowfullscreen></iframe>'
|
| 142 |
return gr.update(value=iframe_html, visible=True)
|
| 143 |
return gr.update(value=None, visible=False)
|
| 144 |
|
| 145 |
css = """
|
| 146 |
-
@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@400;700&display=swap');
|
| 147 |
.blog-output { font-family: 'Sarabun', sans-serif; line-height: 1.8; max-width: 800px; margin: auto; padding: 2rem; border-radius: 12px; background-color: #ffffff; border: 1px solid #e5e7eb; }
|
| 148 |
.blog-output h1 { font-size: 2.2em; font-weight: 700; border-bottom: 2px solid #f3f4f6; padding-bottom: 15px; margin-bottom: 25px; color: #111827; }
|
| 149 |
.blog-output h2 { font-size: 1.6em; font-weight: 700; margin-top: 40px; margin-bottom: 20px; color: #1f2937; }
|
|
@@ -160,7 +168,11 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), cs
|
|
| 160 |
with gr.TabItem("β¬οΈ Upload Audio"):
|
| 161 |
audio_file_input = gr.Audio(label="Upload Audio File", type="filepath")
|
| 162 |
with gr.TabItem("π YouTube Link"):
|
| 163 |
-
youtube_url_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
submit_button = gr.Button("π Generate Blog Post", variant="primary")
|
| 165 |
video_preview = gr.HTML(visible=False)
|
| 166 |
with gr.Accordion("π View Process Log", open=True):
|
|
@@ -174,12 +186,14 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), cs
|
|
| 174 |
submit_button.click(fn=transcribe_and_summarize,
|
| 175 |
inputs=[audio_file_input, youtube_url_input],
|
| 176 |
outputs=[log_output, transcription_output, blog_summary_output])
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
| 185 |
-
demo.launch(debug=True)
|
|
|
|
| 9 |
import uuid
|
| 10 |
import socket
|
| 11 |
|
| 12 |
+
YOUTUBE_REACHABLE = False
|
| 13 |
+
print("--- ATTEMPTING TO RESOLVE YOUTUBE.COM ---")
|
| 14 |
try:
|
| 15 |
addr = socket.gethostbyname('www.youtube.com')
|
| 16 |
+
print(f"--- SUCCESS: 'www.youtube.com' resolved to {addr}. YouTube features enabled. ---")
|
| 17 |
+
YOUTUBE_REACHABLE = True
|
| 18 |
except socket.gaierror as e:
|
| 19 |
+
print(f"--- FAILED to resolve 'www.youtube.com': {e}. YouTube functionality will be disabled. ---")
|
|
|
|
| 20 |
|
| 21 |
print("Initializing transcription model (faster-whisper)...")
|
| 22 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
compute_type = "float16" if device == "cuda" else "int8"
|
| 24 |
+
model_size = "distil-large-v3"
|
| 25 |
+
try:
|
| 26 |
+
model = WhisperModel(model_size, device=device, compute_type=compute_type)
|
| 27 |
+
print("Transcription model loaded successfully.")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Error loading Whisper model: {e}")
|
| 30 |
+
exit()
|
| 31 |
|
| 32 |
def download_youtube_audio(url: str) -> str:
|
| 33 |
unique_id = uuid.uuid4()
|
|
|
|
| 57 |
|
| 58 |
api_key = os.getenv('TYPHOON_API')
|
| 59 |
if not api_key:
|
| 60 |
+
error_msg = "## Error\n`TYPHOON_API` environment variable not set. Please configure the API key."
|
| 61 |
+
yield log(error_msg.replace("\n", " ")), "", gr.Markdown(error_msg)
|
| 62 |
return
|
| 63 |
|
| 64 |
if audio_file is None and not youtube_url:
|
|
|
|
| 118 |
key_takeaway = data.get("key_takeaway", "")
|
| 119 |
main_ideas = data.get("main_ideas", [])
|
| 120 |
conclusion = data.get("conclusion", "")
|
| 121 |
+
|
| 122 |
summary_markdown = f"# {title}\n\n<p>{key_takeaway}</p>\n\n## Key Ideas\n\n<ul>"
|
| 123 |
for idea in main_ideas:
|
| 124 |
summary_markdown += f"<li>{idea}</li>"
|
|
|
|
| 135 |
return gr.update(value=None, visible=False)
|
| 136 |
video_id = None
|
| 137 |
try:
|
| 138 |
+
if "[youtube.com/shorts/](https://youtube.com/shorts/)" in url:
|
| 139 |
video_id = url.split("/shorts/")[1].split("?")[0]
|
| 140 |
elif "watch?v=" in url:
|
| 141 |
video_id = url.split("watch?v=")[1].split("&")[0]
|
|
|
|
| 143 |
video_id = url.split("youtu.be/")[1].split("?")[0]
|
| 144 |
except IndexError:
|
| 145 |
pass
|
| 146 |
+
|
| 147 |
if video_id:
|
| 148 |
+
embed_url = f"[https://www.youtube.com/embed/](https://www.youtube.com/embed/){video_id}"
|
| 149 |
iframe_html = f'<iframe width="100%" height="315" src="{embed_url}" frameborder="0" allowfullscreen></iframe>'
|
| 150 |
return gr.update(value=iframe_html, visible=True)
|
| 151 |
return gr.update(value=None, visible=False)
|
| 152 |
|
| 153 |
css = """
|
| 154 |
+
@import url('[https://fonts.googleapis.com/css2?family=Sarabun:wght@400;700&display=swap](https://fonts.googleapis.com/css2?family=Sarabun:wght@400;700&display=swap)');
|
| 155 |
.blog-output { font-family: 'Sarabun', sans-serif; line-height: 1.8; max-width: 800px; margin: auto; padding: 2rem; border-radius: 12px; background-color: #ffffff; border: 1px solid #e5e7eb; }
|
| 156 |
.blog-output h1 { font-size: 2.2em; font-weight: 700; border-bottom: 2px solid #f3f4f6; padding-bottom: 15px; margin-bottom: 25px; color: #111827; }
|
| 157 |
.blog-output h2 { font-size: 1.6em; font-weight: 700; margin-top: 40px; margin-bottom: 20px; color: #1f2937; }
|
|
|
|
| 168 |
with gr.TabItem("β¬οΈ Upload Audio"):
|
| 169 |
audio_file_input = gr.Audio(label="Upload Audio File", type="filepath")
|
| 170 |
with gr.TabItem("π YouTube Link"):
|
| 171 |
+
youtube_url_input = gr.Textbox(
|
| 172 |
+
label="YouTube URL" if YOUTUBE_REACHABLE else "YouTube URL (Disabled)",
|
| 173 |
+
placeholder="Paste a YouTube link here..." if YOUTUBE_REACHABLE else "YouTube is not reachable in this environment.",
|
| 174 |
+
interactive=YOUTUBE_REACHABLE
|
| 175 |
+
)
|
| 176 |
submit_button = gr.Button("π Generate Blog Post", variant="primary")
|
| 177 |
video_preview = gr.HTML(visible=False)
|
| 178 |
with gr.Accordion("π View Process Log", open=True):
|
|
|
|
| 186 |
submit_button.click(fn=transcribe_and_summarize,
|
| 187 |
inputs=[audio_file_input, youtube_url_input],
|
| 188 |
outputs=[log_output, transcription_output, blog_summary_output])
|
| 189 |
+
|
| 190 |
+
if YOUTUBE_REACHABLE:
|
| 191 |
+
youtube_url_input.change(fn=update_video_preview,
|
| 192 |
+
inputs=youtube_url_input,
|
| 193 |
+
outputs=video_preview)
|
| 194 |
+
demo.load(fn=update_video_preview,
|
| 195 |
+
inputs=youtube_url_input,
|
| 196 |
+
outputs=video_preview)
|
| 197 |
|
| 198 |
if __name__ == "__main__":
|
| 199 |
+
demo.launch(debug=True)
|
requirements.txt
CHANGED
|
@@ -2,4 +2,5 @@ gradio
|
|
| 2 |
torch
|
| 3 |
faster-whisper
|
| 4 |
yt-dlp
|
| 5 |
-
openai
|
|
|
|
|
|
| 2 |
torch
|
| 3 |
faster-whisper
|
| 4 |
yt-dlp
|
| 5 |
+
openai
|
| 6 |
+
hf_transfer
|