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,14 +25,19 @@ Example:
|
|
| 25 |
"""
|
| 26 |
|
| 27 |
def parse_script(full_text):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
scene_parts = re.findall(r'\[SCENE DESCRIPTION\]
|
| 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):
|
|
@@ -55,7 +60,7 @@ def query_groq(topic, tone, duration, hook_strength, chat_history):
|
|
| 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})
|
|
@@ -118,7 +123,7 @@ with gr.Blocks() as demo:
|
|
| 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):
|
|
|
|
| 3 |
import requests
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
# Load GROQ API key from environment
|
| 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 |
+
# Improved regex: Handles optional colons, bolding (**), and case variations
|
| 29 |
+
# Extract [SCRIPT] parts
|
| 30 |
+
script_parts = re.findall(r'\[?SCRIPT\]?:?\s*(.*?)(?=\[?SCENE DESCRIPTION\]?|$)', full_text, re.DOTALL | re.IGNORECASE)
|
| 31 |
+
clean_script = "\n".join([p.strip().replace("**", "") for p in script_parts if p.strip()])
|
| 32 |
|
| 33 |
+
# Extract [SCENE DESCRIPTION] parts
|
| 34 |
+
scene_parts = re.findall(r'\[?SCENE DESCRIPTION\]?:?\s*(.*?)(?=\[?SCRIPT\]?|$)', full_text, re.DOTALL | re.IGNORECASE)
|
| 35 |
+
clean_scenes = "\n".join([p.strip().replace("**", "") for p in scene_parts if p.strip()])
|
| 36 |
|
| 37 |
+
# Fallback: If parsing fails but we have text, assume the whole thing is the script
|
| 38 |
+
if not clean_script and full_text:
|
| 39 |
+
clean_script = full_text
|
| 40 |
+
|
| 41 |
return clean_script, clean_scenes
|
| 42 |
|
| 43 |
def save_to_file(script_text):
|
|
|
|
| 60 |
user_input = f"Topic: {topic}\nTone: {tone}\nTarget Duration: {duration}\nAction: Write a full YouTube script."
|
| 61 |
|
| 62 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 63 |
+
# Add history for context (keep last 6 messages / 3 turns)
|
| 64 |
messages.extend(chat_history[-6:])
|
| 65 |
|
| 66 |
messages.append({"role": "user", "content": user_input})
|
|
|
|
| 123 |
with gr.TabItem("Visuals Only (Shot List)"):
|
| 124 |
scenes_output = gr.Textbox(label="Video Scene Descriptions", lines=20)
|
| 125 |
|
| 126 |
+
# State for history (initially contains the welcome message)
|
| 127 |
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."}])
|
| 128 |
|
| 129 |
def respond_wrapper(topic, tone, duration, hook_strength, chat_history):
|