Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import os
|
|
| 3 |
import requests
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
|
| 7 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 8 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 9 |
MODEL_NAME = "llama-3.1-8b-instant"
|
|
@@ -25,23 +25,15 @@ Example:
|
|
| 25 |
"""
|
| 26 |
|
| 27 |
def parse_script(full_text):
|
| 28 |
-
|
| 29 |
script_parts = re.findall(r'\[SCRIPT\]:(.*?)(?=\[SCENE DESCRIPTION\]|$)', full_text, re.DOTALL | re.IGNORECASE)
|
| 30 |
clean_script = "\n".join([p.strip() for p in script_parts])
|
| 31 |
|
| 32 |
-
|
| 33 |
scene_parts = re.findall(r'\[SCENE DESCRIPTION\]:(.*?)(?=\[SCRIPT\]|$)', full_text, re.DOTALL | re.IGNORECASE)
|
| 34 |
clean_scenes = "\n".join([p.strip() for p in scene_parts])
|
| 35 |
|
| 36 |
-
|
| 37 |
-
word_count = len(clean_script.split())
|
| 38 |
-
# Est duration: ~150 words per minute
|
| 39 |
-
duration_minutes = word_count / 150
|
| 40 |
-
minutes = int(duration_minutes)
|
| 41 |
-
seconds = int((duration_minutes - minutes) * 60)
|
| 42 |
-
duration_str = f"{minutes}m {seconds}s"
|
| 43 |
-
|
| 44 |
-
return clean_script, clean_scenes, word_count, duration_str
|
| 45 |
|
| 46 |
def save_to_file(script_text):
|
| 47 |
if not script_text:
|
|
@@ -53,7 +45,7 @@ def save_to_file(script_text):
|
|
| 53 |
|
| 54 |
def query_groq(topic, tone, duration, hook_strength, chat_history):
|
| 55 |
if not GROQ_API_KEY:
|
| 56 |
-
return "Error: GROQ_API_KEY not found in environment secrets. Please add it in Settings > Secrets.", "", ""
|
| 57 |
|
| 58 |
headers = {
|
| 59 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
@@ -63,7 +55,7 @@ def query_groq(topic, tone, duration, hook_strength, chat_history):
|
|
| 63 |
user_input = f"Topic: {topic}\nTone: {tone}\nTarget Duration: {duration}\nAction: Write a full YouTube script."
|
| 64 |
|
| 65 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 66 |
-
|
| 67 |
messages.extend(chat_history[-6:])
|
| 68 |
|
| 69 |
messages.append({"role": "user", "content": user_input})
|
|
@@ -77,28 +69,16 @@ def query_groq(topic, tone, duration, hook_strength, chat_history):
|
|
| 77 |
|
| 78 |
if response.status_code == 200:
|
| 79 |
full_reply = response.json()["choices"][0]["message"]["content"]
|
| 80 |
-
tts_script, scenes
|
| 81 |
-
return full_reply, tts_script, scenes
|
| 82 |
else:
|
| 83 |
-
return f"Error {response.status_code}: {response.text}", "", ""
|
| 84 |
except Exception as e:
|
| 85 |
-
return f"Request failed: {str(e)}", "", ""
|
| 86 |
|
| 87 |
-
def respond(topic, tone, duration, hook_strength, chat_history):
|
| 88 |
-
full_reply, tts_script, scenes, wc, dur = query_groq(topic, tone, duration, hook_strength, chat_history)
|
| 89 |
-
chat_history.append({"role": "user", "content": topic})
|
| 90 |
-
chat_history.append({"role": "assistant", "content": full_reply})
|
| 91 |
-
return chat_history, tts_script, scenes, f"{wc} words", dur
|
| 92 |
|
| 93 |
css = """
|
| 94 |
footer {visibility: hidden}
|
| 95 |
-
.stat-box {
|
| 96 |
-
background-color: #f0f2f6;
|
| 97 |
-
padding: 10px;
|
| 98 |
-
border-radius: 10px;
|
| 99 |
-
text-align: center;
|
| 100 |
-
border: 1px solid #ddd;
|
| 101 |
-
}
|
| 102 |
"""
|
| 103 |
|
| 104 |
with gr.Blocks() as demo:
|
|
@@ -123,13 +103,12 @@ with gr.Blocks() as demo:
|
|
| 123 |
clear = gr.Button("Clear")
|
| 124 |
|
| 125 |
with gr.Column(scale=2):
|
| 126 |
-
with gr.Row():
|
| 127 |
-
word_count_display = gr.Textbox(label="Word Count", interactive=False, elem_classes="stat-box")
|
| 128 |
-
duration_display = gr.Textbox(label="Est. Speaking Time", interactive=False, elem_classes="stat-box")
|
| 129 |
-
|
| 130 |
with gr.Tabs():
|
| 131 |
with gr.TabItem("Combined View"):
|
| 132 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
with gr.TabItem("TTS Only (Dialogue)"):
|
| 135 |
tts_output = gr.Textbox(label="Copy this for Text-to-Speech", lines=20)
|
|
@@ -139,13 +118,19 @@ with gr.Blocks() as demo:
|
|
| 139 |
with gr.TabItem("Visuals Only (Shot List)"):
|
| 140 |
scenes_output = gr.Textbox(label="Video Scene Descriptions", lines=20)
|
| 141 |
|
| 142 |
-
|
| 143 |
-
state = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
generate_btn.click(
|
| 146 |
-
|
| 147 |
[topic, tone, duration, hook_strength, state],
|
| 148 |
-
[chatbot, tts_output, scenes_output
|
| 149 |
)
|
| 150 |
|
| 151 |
download_btn.click(
|
|
@@ -154,7 +139,11 @@ with gr.Blocks() as demo:
|
|
| 154 |
[download_file]
|
| 155 |
)
|
| 156 |
|
| 157 |
-
clear.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
| 160 |
demo.launch(theme=gr.themes.Soft(), css=css)
|
|
|
|
| 3 |
import requests
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
|
| 7 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 8 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 9 |
MODEL_NAME = "llama-3.1-8b-instant"
|
|
|
|
| 25 |
"""
|
| 26 |
|
| 27 |
def parse_script(full_text):
|
| 28 |
+
|
| 29 |
script_parts = re.findall(r'\[SCRIPT\]:(.*?)(?=\[SCENE DESCRIPTION\]|$)', full_text, re.DOTALL | re.IGNORECASE)
|
| 30 |
clean_script = "\n".join([p.strip() for p in script_parts])
|
| 31 |
|
| 32 |
+
|
| 33 |
scene_parts = re.findall(r'\[SCENE DESCRIPTION\]:(.*?)(?=\[SCRIPT\]|$)', full_text, re.DOTALL | re.IGNORECASE)
|
| 34 |
clean_scenes = "\n".join([p.strip() for p in scene_parts])
|
| 35 |
|
| 36 |
+
return clean_script, clean_scenes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def save_to_file(script_text):
|
| 39 |
if not script_text:
|
|
|
|
| 45 |
|
| 46 |
def query_groq(topic, tone, duration, hook_strength, chat_history):
|
| 47 |
if not GROQ_API_KEY:
|
| 48 |
+
return "Error: GROQ_API_KEY not found in environment secrets. Please add it in Settings > Secrets.", "", ""
|
| 49 |
|
| 50 |
headers = {
|
| 51 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
|
|
| 55 |
user_input = f"Topic: {topic}\nTone: {tone}\nTarget Duration: {duration}\nAction: Write a full YouTube script."
|
| 56 |
|
| 57 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 58 |
+
|
| 59 |
messages.extend(chat_history[-6:])
|
| 60 |
|
| 61 |
messages.append({"role": "user", "content": user_input})
|
|
|
|
| 69 |
|
| 70 |
if response.status_code == 200:
|
| 71 |
full_reply = response.json()["choices"][0]["message"]["content"]
|
| 72 |
+
tts_script, scenes = parse_script(full_reply)
|
| 73 |
+
return full_reply, tts_script, scenes
|
| 74 |
else:
|
| 75 |
+
return f"Error {response.status_code}: {response.text}", "", ""
|
| 76 |
except Exception as e:
|
| 77 |
+
return f"Request failed: {str(e)}", "", ""
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
css = """
|
| 81 |
footer {visibility: hidden}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
"""
|
| 83 |
|
| 84 |
with gr.Blocks() as demo:
|
|
|
|
| 103 |
clear = gr.Button("Clear")
|
| 104 |
|
| 105 |
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
with gr.Tabs():
|
| 107 |
with gr.TabItem("Combined View"):
|
| 108 |
+
chatbot = gr.Chatbot(
|
| 109 |
+
value=[{"role": "assistant", "content": "Hi, my name is Script Forge: your YouTube script writer. Give me a topic so I can show my creativity."}],
|
| 110 |
+
height=500
|
| 111 |
+
)
|
| 112 |
|
| 113 |
with gr.TabItem("TTS Only (Dialogue)"):
|
| 114 |
tts_output = gr.Textbox(label="Copy this for Text-to-Speech", lines=20)
|
|
|
|
| 118 |
with gr.TabItem("Visuals Only (Shot List)"):
|
| 119 |
scenes_output = gr.Textbox(label="Video Scene Descriptions", lines=20)
|
| 120 |
|
| 121 |
+
|
| 122 |
+
state = gr.State([{"role": "assistant", "content": "Hi, my name is Script Forge: your YouTube script writer. Give me a topic so I can show my creativity."}])
|
| 123 |
+
|
| 124 |
+
def respond_wrapper(topic, tone, duration, hook_strength, chat_history):
|
| 125 |
+
full_reply, tts_script, scenes = query_groq(topic, tone, duration, hook_strength, chat_history)
|
| 126 |
+
chat_history.append({"role": "user", "content": topic})
|
| 127 |
+
chat_history.append({"role": "assistant", "content": full_reply})
|
| 128 |
+
return chat_history, tts_script, scenes
|
| 129 |
|
| 130 |
generate_btn.click(
|
| 131 |
+
respond_wrapper,
|
| 132 |
[topic, tone, duration, hook_strength, state],
|
| 133 |
+
[chatbot, tts_output, scenes_output]
|
| 134 |
)
|
| 135 |
|
| 136 |
download_btn.click(
|
|
|
|
| 139 |
[download_file]
|
| 140 |
)
|
| 141 |
|
| 142 |
+
clear.click(
|
| 143 |
+
lambda: (None, [{"role": "assistant", "content": "Hi, my name is Script Forge: your YouTube script writer. Give me a topic so I can show my creativity."}], "", "", None),
|
| 144 |
+
None,
|
| 145 |
+
[topic, chatbot, tts_output, scenes_output, download_file]
|
| 146 |
+
)
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
| 149 |
demo.launch(theme=gr.themes.Soft(), css=css)
|