Ansnaeem commited on
Commit
cb6a767
·
verified ·
1 Parent(s): 041a66a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -3,7 +3,7 @@ import os
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"
@@ -24,11 +24,11 @@ FORMATTING RULES (STRICT):
24
  """
25
 
26
  def parse_script(full_text):
27
- # Normalize tags just in case
28
  full_text = re.sub(r'\[?(?:SCENE DESCRIPTION|SCENE|VISUALS)\]?:?', '[VISUAL]', full_text, flags=re.IGNORECASE)
29
  full_text = re.sub(r'\[?(?:SCRIPT|NARRATION|AUDIO)\]?:?', '[AUDIO]', full_text, flags=re.IGNORECASE)
30
 
31
- # Split by tags
32
  parts = re.split(r'(\[(?:VISUAL|AUDIO)\])', full_text, flags=re.IGNORECASE)
33
 
34
  clean_audio = []
@@ -46,14 +46,13 @@ def parse_script(full_text):
46
  elif part.upper() == "[VISUAL]":
47
  current_tag = "VISUAL"
48
  elif current_tag == "AUDIO":
49
- # Clean Audio
50
- # Remove parenthetical notes like (whispering)
51
  content = re.sub(r'\(.*?\)', '', part, flags=re.DOTALL)
52
- # Remove headers
53
  content = re.sub(r'^#+.*$', '', content, flags=re.MULTILINE)
54
- # Remove labels like "Host:"
55
  content = re.sub(r'^\w+:\s*', '', content, flags=re.MULTILINE)
56
- # Remove formatting
57
  content = content.replace("**", "").replace("*", "")
58
 
59
  if content.strip():
@@ -62,7 +61,7 @@ def parse_script(full_text):
62
  elif current_tag == "VISUAL":
63
  clean_visuals.append(part.strip())
64
 
65
- # Fallback: If no tags found, attempt heuristic split
66
  if not clean_audio and not clean_visuals:
67
  lines = full_text.split('\n')
68
  for line in lines:
@@ -95,7 +94,7 @@ def query_groq(topic, tone, duration, hook_strength, chat_history):
95
  user_input = f"Topic: {topic}\nTone: {tone}\nTarget Duration: {duration}\nAction: Write a full YouTube script."
96
 
97
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
98
- # Add history for context (keep last 6 messages / 3 turns)
99
  messages.extend(chat_history[-6:])
100
 
101
  messages.append({"role": "user", "content": user_input})
@@ -158,7 +157,7 @@ with gr.Blocks() as demo:
158
  with gr.TabItem("Visuals Only (Shot List)"):
159
  scenes_output = gr.Textbox(label="Video Scene Descriptions", lines=20)
160
 
161
- # State for history (initially contains the welcome message)
162
  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."}])
163
 
164
  def respond_wrapper(topic, tone, duration, hook_strength, chat_history):
 
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"
 
24
  """
25
 
26
  def parse_script(full_text):
27
+
28
  full_text = re.sub(r'\[?(?:SCENE DESCRIPTION|SCENE|VISUALS)\]?:?', '[VISUAL]', full_text, flags=re.IGNORECASE)
29
  full_text = re.sub(r'\[?(?:SCRIPT|NARRATION|AUDIO)\]?:?', '[AUDIO]', full_text, flags=re.IGNORECASE)
30
 
31
+
32
  parts = re.split(r'(\[(?:VISUAL|AUDIO)\])', full_text, flags=re.IGNORECASE)
33
 
34
  clean_audio = []
 
46
  elif part.upper() == "[VISUAL]":
47
  current_tag = "VISUAL"
48
  elif current_tag == "AUDIO":
49
+
 
50
  content = re.sub(r'\(.*?\)', '', part, flags=re.DOTALL)
51
+
52
  content = re.sub(r'^#+.*$', '', content, flags=re.MULTILINE)
53
+
54
  content = re.sub(r'^\w+:\s*', '', content, flags=re.MULTILINE)
55
+
56
  content = content.replace("**", "").replace("*", "")
57
 
58
  if content.strip():
 
61
  elif current_tag == "VISUAL":
62
  clean_visuals.append(part.strip())
63
 
64
+
65
  if not clean_audio and not clean_visuals:
66
  lines = full_text.split('\n')
67
  for line in lines:
 
94
  user_input = f"Topic: {topic}\nTone: {tone}\nTarget Duration: {duration}\nAction: Write a full YouTube script."
95
 
96
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
97
+
98
  messages.extend(chat_history[-6:])
99
 
100
  messages.append({"role": "user", "content": user_input})
 
157
  with gr.TabItem("Visuals Only (Shot List)"):
158
  scenes_output = gr.Textbox(label="Video Scene Descriptions", lines=20)
159
 
160
+
161
  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."}])
162
 
163
  def respond_wrapper(topic, tone, duration, hook_strength, chat_history):