Update app.py
Browse files
app.py
CHANGED
@@ -37,6 +37,7 @@ def clean_response_text(response_text):
|
|
37 |
def generate_ideas(user_input):
|
38 |
"""
|
39 |
Generate a diverse set of ideas based on the user's input concept using the LLM.
|
|
|
40 |
|
41 |
Args:
|
42 |
user_input (str): The user's input concept or idea (e.g., "blindfolded Rubik's Cube challenge").
|
@@ -44,6 +45,7 @@ def generate_ideas(user_input):
|
|
44 |
Returns:
|
45 |
list: A list of ideas as strings.
|
46 |
"""
|
|
|
47 |
prompt = f"""
|
48 |
The user has provided the concept: "{user_input}". You must generate 5 diverse and creative ideas for a TikTok video that are directly and explicitly related to "{user_input}".
|
49 |
Each idea must clearly incorporate and focus on the core theme of "{user_input}" without deviating into unrelated topics.
|
@@ -73,11 +75,43 @@ def generate_ideas(user_input):
|
|
73 |
if 'ideas' not in response_json or not isinstance(response_json['ideas'], list) or len(response_json['ideas']) != 5:
|
74 |
raise ValueError("Invalid JSON format: 'ideas' key missing, not a list, or incorrect length")
|
75 |
|
76 |
-
# Validate that ideas are related to user_input
|
77 |
ideas = response_json['ideas']
|
|
|
|
|
78 |
for idea in ideas:
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
return [
|
82 |
f"A dramatic {user_input} scene with cinematic lighting",
|
83 |
f"A close-up of {user_input} in a futuristic setting",
|
@@ -85,7 +119,10 @@ def generate_ideas(user_input):
|
|
85 |
f"A serene {user_input} scene with soft focus",
|
86 |
f"An action-packed {user_input} challenge with dynamic angles"
|
87 |
]
|
|
|
|
|
88 |
return ideas
|
|
|
89 |
except Exception as e:
|
90 |
print(f"Error generating ideas: {e}")
|
91 |
return [
|
@@ -824,7 +861,7 @@ with gr.Blocks(
|
|
824 |
# Handle magic button click to generate next item
|
825 |
magic_button.click(
|
826 |
fn=load_next,
|
827 |
-
inputs=[user_input, generate_video_checkbox, current_index, feed_items],
|
828 |
outputs=[current_user_input, current_index, feed_items, feed_html, share_html, is_loading]
|
829 |
)
|
830 |
|
|
|
37 |
def generate_ideas(user_input):
|
38 |
"""
|
39 |
Generate a diverse set of ideas based on the user's input concept using the LLM.
|
40 |
+
Validate the relevance of each idea using a cheaper LLM (gemini-2.0-flash-lite).
|
41 |
|
42 |
Args:
|
43 |
user_input (str): The user's input concept or idea (e.g., "blindfolded Rubik's Cube challenge").
|
|
|
45 |
Returns:
|
46 |
list: A list of ideas as strings.
|
47 |
"""
|
48 |
+
# Step 1: Generate ideas using gemini-2.0-flash
|
49 |
prompt = f"""
|
50 |
The user has provided the concept: "{user_input}". You must generate 5 diverse and creative ideas for a TikTok video that are directly and explicitly related to "{user_input}".
|
51 |
Each idea must clearly incorporate and focus on the core theme of "{user_input}" without deviating into unrelated topics.
|
|
|
75 |
if 'ideas' not in response_json or not isinstance(response_json['ideas'], list) or len(response_json['ideas']) != 5:
|
76 |
raise ValueError("Invalid JSON format: 'ideas' key missing, not a list, or incorrect length")
|
77 |
|
|
|
78 |
ideas = response_json['ideas']
|
79 |
+
|
80 |
+
# Step 2: Validate relevance of each idea using gemini-2.0-flash-lite
|
81 |
for idea in ideas:
|
82 |
+
validation_prompt = f"""
|
83 |
+
Determine if the following idea for a TikTok video is related to the user's concept.
|
84 |
+
User's concept: "{user_input}"
|
85 |
+
Idea: "{idea}"
|
86 |
+
Respond with a JSON object containing a single key 'is_related' with a boolean value (true or false).
|
87 |
+
Example:
|
88 |
+
{{"is_related": true}}
|
89 |
+
"""
|
90 |
+
try:
|
91 |
+
validation_response = client.models.generate_content(
|
92 |
+
model='gemini-2.0-flash-lite',
|
93 |
+
contents=[validation_prompt],
|
94 |
+
config=types.GenerateContentConfig(temperature=0.0) # Low temperature for deterministic output
|
95 |
+
)
|
96 |
+
print(f"Validation response for idea '{idea}': {validation_response.text}") # Debugging
|
97 |
+
if not validation_response.text or validation_response.text.isspace():
|
98 |
+
raise ValueError("Empty validation response from API")
|
99 |
+
cleaned_validation_text = clean_response_text(validation_response.text)
|
100 |
+
validation_json = json.loads(cleaned_validation_text)
|
101 |
+
if 'is_related' not in validation_json or not isinstance(validation_json['is_related'], bool):
|
102 |
+
raise ValueError("Invalid validation JSON format: 'is_related' key missing or not a boolean")
|
103 |
+
|
104 |
+
if not validation_json['is_related']:
|
105 |
+
print(f"Idea '{idea}' is not related to '{user_input}'. Falling back to default ideas.")
|
106 |
+
return [
|
107 |
+
f"A dramatic {user_input} scene with cinematic lighting",
|
108 |
+
f"A close-up of {user_input} in a futuristic setting",
|
109 |
+
f"A high-energy {user_input} moment with vibrant colors",
|
110 |
+
f"A serene {user_input} scene with soft focus",
|
111 |
+
f"An action-packed {user_input} challenge with dynamic angles"
|
112 |
+
]
|
113 |
+
except Exception as e:
|
114 |
+
print(f"Error validating idea '{idea}': {e}. Falling back to default ideas.")
|
115 |
return [
|
116 |
f"A dramatic {user_input} scene with cinematic lighting",
|
117 |
f"A close-up of {user_input} in a futuristic setting",
|
|
|
119 |
f"A serene {user_input} scene with soft focus",
|
120 |
f"An action-packed {user_input} challenge with dynamic angles"
|
121 |
]
|
122 |
+
|
123 |
+
# All ideas are related, return them
|
124 |
return ideas
|
125 |
+
|
126 |
except Exception as e:
|
127 |
print(f"Error generating ideas: {e}")
|
128 |
return [
|
|
|
861 |
# Handle magic button click to generate next item
|
862 |
magic_button.click(
|
863 |
fn=load_next,
|
864 |
+
inputs=[user_input, generate_video_checkbox, current_index, feed_items],
|
865 |
outputs=[current_user_input, current_index, feed_items, feed_html, share_html, is_loading]
|
866 |
)
|
867 |
|