Spaces:
Runtime error
Runtime error
jacklangerman
commited on
Commit
β’
86bfa3b
1
Parent(s):
c38d00f
prepare app
Browse files- app.py +31 -0
- tok2text.py +10 -8
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from tok2text import extract_recipe
|
3 |
+
|
4 |
+
def process_tiktok_url(tiktok_url):
|
5 |
+
try:
|
6 |
+
formatted_recipe, formatted_recipe_path, transcript_path = extract_recipe(tiktok_url)
|
7 |
+
return formatted_recipe, str(formatted_recipe_path), str(transcript_path)
|
8 |
+
except Exception as e:
|
9 |
+
return f"Error: {str(e)}", None
|
10 |
+
|
11 |
+
demo = gr.Interface(
|
12 |
+
fn=process_tiktok_url,
|
13 |
+
inputs=gr.Textbox(label="TikTok URL"),
|
14 |
+
outputs=[
|
15 |
+
gr.Textbox(label="Formatted Recipe"),
|
16 |
+
gr.File(label="Download Formatted Recipe"),
|
17 |
+
gr.File(label="Download Raw Transcript"),
|
18 |
+
],
|
19 |
+
title="TikTok Recipe Extractor",
|
20 |
+
description="Extract and format recipes from TikTok videos.",
|
21 |
+
examples=[
|
22 |
+
["https://www.tiktok.com/t/ZTLjYBSpt/"],
|
23 |
+
["https://www.tiktok.com/@emmaaaaaaam_/video/7348493781961886981"]
|
24 |
+
],
|
25 |
+
allow_flagging="never"
|
26 |
+
)
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
if __name__ == "__main__":
|
31 |
+
demo.launch()
|
tok2text.py
CHANGED
@@ -56,7 +56,7 @@ def transcribe_audio(audio_file_path):
|
|
56 |
print('Transcript already exists. Skipping transcription.')
|
57 |
with transcript_path.open() as f:
|
58 |
transcription = f.read()
|
59 |
-
return transcription
|
60 |
|
61 |
def format_recipe(transcript, output_path, tiktok_url):
|
62 |
formatted_recipe_path = output_path / 'formatted_recipe.txt'
|
@@ -65,7 +65,7 @@ def format_recipe(transcript, output_path, tiktok_url):
|
|
65 |
|
66 |
response = client.chat.completions.create(model="gpt-3.5-turbo",
|
67 |
messages=[
|
68 |
-
{"role": "system", "content": "You are a helpful assistant that turns transcripts of TikTok recipe videos into nicely formatted recipes. Please output the recipe only and now additional text or cometary. Each recipe should have exactly three sections: Title, Ingredients, and Instructions. Make sure to write every step and ingredient and if you're not sure about something make sure to write a note in parentheses explaining why you are unsure, and how you guessed, prepend 'β' to the amount, make the best estimation you can given the context."},
|
69 |
{"role": "user", "content": prompt}
|
70 |
],
|
71 |
max_tokens=500,
|
@@ -98,18 +98,20 @@ def expand_url(short_url):
|
|
98 |
|
99 |
def extract_recipe(tiktok_url):
|
100 |
tiktok_url = expand_url(tiktok_url)
|
101 |
-
|
102 |
-
|
|
|
|
|
103 |
|
104 |
output_path = Path(video_id)
|
105 |
output_path.mkdir(parents=True, exist_ok=True)
|
106 |
|
107 |
-
video_path = download_video(
|
108 |
audio_path = extract_audio(video_path)
|
109 |
-
transcript = transcribe_audio(audio_path)
|
110 |
-
formatted_recipe, formatted_recipe_path = format_recipe(transcript, output_path,
|
111 |
|
112 |
-
return formatted_recipe, formatted_recipe_path
|
113 |
|
114 |
def main():
|
115 |
# tiktok_url = "https://www.tiktok.com/@emmaaaaaaam_/video/7348493781961886981"
|
|
|
56 |
print('Transcript already exists. Skipping transcription.')
|
57 |
with transcript_path.open() as f:
|
58 |
transcription = f.read()
|
59 |
+
return transcription, transcript_path
|
60 |
|
61 |
def format_recipe(transcript, output_path, tiktok_url):
|
62 |
formatted_recipe_path = output_path / 'formatted_recipe.txt'
|
|
|
65 |
|
66 |
response = client.chat.completions.create(model="gpt-3.5-turbo",
|
67 |
messages=[
|
68 |
+
{"role": "system", "content": "You are a helpful assistant that turns transcripts of TikTok recipe videos into nicely formatted recipes. Please output the recipe only and now additional text or cometary. Each recipe should have exactly three sections: Title, Ingredients, and Instructions. Make sure to write every step and ingredient and if you're not sure about something make sure to write a note in parentheses explaining why you are unsure, and how you guessed, prepend 'β' to the amount, make the best estimation you can given the context. You may also optionally add a note at the end of the recipe."},
|
69 |
{"role": "user", "content": prompt}
|
70 |
],
|
71 |
max_tokens=500,
|
|
|
98 |
|
99 |
def extract_recipe(tiktok_url):
|
100 |
tiktok_url = expand_url(tiktok_url)
|
101 |
+
tiktok_url_full = tiktok_url.strip().split("?")[0].strip()
|
102 |
+
tiktok_url_clean = tiktok_url_full.lstrip('https://').lstrip('http://').lstrip('www.')
|
103 |
+
|
104 |
+
_, user, _, video_id = tiktok_url_clean.split("/")
|
105 |
|
106 |
output_path = Path(video_id)
|
107 |
output_path.mkdir(parents=True, exist_ok=True)
|
108 |
|
109 |
+
video_path = download_video(tiktok_url_full, video_id, output_path)
|
110 |
audio_path = extract_audio(video_path)
|
111 |
+
transcript, transcript_path = transcribe_audio(audio_path)
|
112 |
+
formatted_recipe, formatted_recipe_path = format_recipe(transcript, output_path, tiktok_url_clean)
|
113 |
|
114 |
+
return formatted_recipe, formatted_recipe_path, transcript_path
|
115 |
|
116 |
def main():
|
117 |
# tiktok_url = "https://www.tiktok.com/@emmaaaaaaam_/video/7348493781961886981"
|