Spaces:
Sleeping
Sleeping
1. improved system prompt for context aware prompts
Browse files
app.py
CHANGED
@@ -2,43 +2,35 @@ import streamlit as st
|
|
2 |
import os
|
3 |
import tempfile
|
4 |
import uuid
|
|
|
5 |
from utils import get_translation, get_image_prompts, segments_to_chunks, generate_images, generate_video
|
6 |
import constants
|
7 |
from groq import Groq
|
8 |
|
|
|
|
|
|
|
9 |
|
10 |
client = Groq()
|
11 |
|
12 |
# Generate a unique session ID for each user
|
13 |
if 'session_id' not in st.session_state:
|
14 |
st.session_state.session_id = str(uuid.uuid4())
|
|
|
15 |
|
16 |
session_id = st.session_state.session_id
|
17 |
|
18 |
# Initialize state variables if not already set
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
st.session_state[f'uploaded_file_name_{session_id}'] = None
|
25 |
-
if f'audio_{session_id}' not in st.session_state:
|
26 |
-
st.session_state[f'audio_{session_id}'] = None
|
27 |
-
if f'was_converted_{session_id}' not in st.session_state:
|
28 |
-
st.session_state[f'was_converted_{session_id}'] = False
|
29 |
-
if f'transcript_{session_id}' not in st.session_state:
|
30 |
-
st.session_state[f'transcript_{session_id}'] = None
|
31 |
-
if f'translation_{session_id}' not in st.session_state:
|
32 |
-
st.session_state[f'translation_{session_id}'] = None
|
33 |
-
if f'generated_video_{session_id}' not in st.session_state:
|
34 |
-
st.session_state[f'generated_video_{session_id}'] = None
|
35 |
-
if f'image_prompts_{session_id}' not in st.session_state:
|
36 |
-
st.session_state[f'image_prompts_{session_id}'] = None
|
37 |
-
if f'generated_images_{session_id}' not in st.session_state:
|
38 |
-
st.session_state[f'generated_images_{session_id}'] = None
|
39 |
-
if f'video_generated_{session_id}' not in st.session_state:
|
40 |
-
st.session_state[f'video_generated_{session_id}'] = False
|
41 |
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Streamlit UI
|
44 |
st.markdown(
|
@@ -49,7 +41,6 @@ st.markdown("<p style='text-align: center;'>Leave a Like if it works for you!
|
|
49 |
st.info("**Video Generation Feature** - Functional But Can be Buggy")
|
50 |
|
51 |
# Encourage users to like the app
|
52 |
-
|
53 |
audio_option = st.radio("Choose audio input method:", ("Upload Audio File", "Record Audio"), horizontal=True)
|
54 |
|
55 |
if audio_option == "Upload Audio File":
|
@@ -58,10 +49,11 @@ if audio_option == "Upload Audio File":
|
|
58 |
else:
|
59 |
audio_file = st.audio_input("🔊 Record Audio")
|
60 |
|
61 |
-
|
62 |
-
|
63 |
|
64 |
if audio_file:
|
|
|
|
|
65 |
# Reset states only when a new file is uploaded
|
66 |
if st.session_state[f'uploaded_file_name_{session_id}'] != audio_file.name:
|
67 |
st.session_state[f'uploaded_file_name_{session_id}'] = audio_file.name
|
@@ -72,27 +64,33 @@ if audio_file:
|
|
72 |
st.session_state[f'generated_images_{session_id}'] = None # Reset image generation state
|
73 |
st.session_state[f'generated_video_{session_id}'] = None # Reset generated video state
|
74 |
st.session_state[f'video_generated_{session_id}'] = False # Reset video generated flag
|
75 |
-
|
76 |
-
st.info(f"Uploaded file: **{audio_file.name}**")
|
77 |
|
78 |
# Read the uploaded file's bytes and send to Groq API for transcription
|
79 |
file_bytes = audio_file.read()
|
|
|
80 |
|
81 |
# Create a transcription of the audio file using Groq API
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
# Translation logic
|
93 |
if st.session_state[f'transcript_{session_id}'] and st.session_state[f'translation_{session_id}'] is None:
|
94 |
with st.spinner("Generating translation... Please wait."):
|
95 |
st.session_state[f'translation_{session_id}'] = get_translation(st.session_state[f'transcript_{session_id}'])
|
|
|
96 |
|
97 |
st.audio(st.session_state[f'audio_{session_id}'], format=f"audio/{audio_file.type}")
|
98 |
|
@@ -120,8 +118,8 @@ if audio_file:
|
|
120 |
st.session_state[f'image_prompts_{session_id}'] = get_image_prompts(segments_to_chunks(st.session_state[f'segments_{session_id}']))['image_prompts']
|
121 |
else:
|
122 |
st.session_state[f'image_prompts_{session_id}'] = get_image_prompts(segments_to_chunks(st.session_state[f'segments_{session_id}']))['image_prompts']
|
|
|
123 |
|
124 |
-
print(st.session_state[f'image_prompts_{session_id}'])
|
125 |
# Ensure that generated_images is always a list
|
126 |
if f'generated_images_{session_id}' not in st.session_state or st.session_state[f'generated_images_{session_id}'] is None:
|
127 |
st.session_state[f'generated_images_{session_id}'] = []
|
@@ -141,6 +139,7 @@ if audio_file:
|
|
141 |
|
142 |
progress_placeholder.text("✅ All images generated successfully!")
|
143 |
progress_bar.empty()
|
|
|
144 |
|
145 |
# Generate video when all images are generated
|
146 |
if st.session_state[f'generated_images_{session_id}'] and st.session_state[f'audio_{session_id}'] and not st.session_state[f'video_generated_{session_id}']:
|
@@ -160,6 +159,7 @@ if audio_file:
|
|
160 |
st.session_state[f'generated_video_{session_id}'] = generated_video_path
|
161 |
st.session_state[f'video_generated_{session_id}'] = True # Set the flag to True
|
162 |
st.success("Video generated successfully!")
|
|
|
163 |
|
164 |
# Display the generated video
|
165 |
if st.session_state[f'generated_video_{session_id}']:
|
@@ -176,5 +176,6 @@ if audio_file:
|
|
176 |
|
177 |
else:
|
178 |
st.warning("Please upload an audio file to proceed.")
|
|
|
179 |
|
180 |
|
|
|
2 |
import os
|
3 |
import tempfile
|
4 |
import uuid
|
5 |
+
import logging
|
6 |
from utils import get_translation, get_image_prompts, segments_to_chunks, generate_images, generate_video
|
7 |
import constants
|
8 |
from groq import Groq
|
9 |
|
10 |
+
# Set up logging
|
11 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
12 |
+
logger = logging.getLogger()
|
13 |
|
14 |
client = Groq()
|
15 |
|
16 |
# Generate a unique session ID for each user
|
17 |
if 'session_id' not in st.session_state:
|
18 |
st.session_state.session_id = str(uuid.uuid4())
|
19 |
+
logger.info(f"New session created with ID: {st.session_state.session_id}")
|
20 |
|
21 |
session_id = st.session_state.session_id
|
22 |
|
23 |
# Initialize state variables if not already set
|
24 |
+
state_variables = [
|
25 |
+
'transcript_visible', 'translation_visible', 'uploaded_file_name',
|
26 |
+
'audio', 'was_converted', 'transcript', 'translation',
|
27 |
+
'generated_video', 'image_prompts', 'generated_images', 'video_generated'
|
28 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
for var in state_variables:
|
31 |
+
if f'{var}_{session_id}' not in st.session_state:
|
32 |
+
st.session_state[f'{var}_{session_id}'] = None
|
33 |
+
logger.info(f"Initialized state variable: {var}_{session_id}")
|
34 |
|
35 |
# Streamlit UI
|
36 |
st.markdown(
|
|
|
41 |
st.info("**Video Generation Feature** - Functional But Can be Buggy")
|
42 |
|
43 |
# Encourage users to like the app
|
|
|
44 |
audio_option = st.radio("Choose audio input method:", ("Upload Audio File", "Record Audio"), horizontal=True)
|
45 |
|
46 |
if audio_option == "Upload Audio File":
|
|
|
49 |
else:
|
50 |
audio_file = st.audio_input("🔊 Record Audio")
|
51 |
|
52 |
+
logger.debug(f"Audio option selected: {audio_option}")
|
|
|
53 |
|
54 |
if audio_file:
|
55 |
+
logger.info(f"Audio file received: {audio_file.name}")
|
56 |
+
|
57 |
# Reset states only when a new file is uploaded
|
58 |
if st.session_state[f'uploaded_file_name_{session_id}'] != audio_file.name:
|
59 |
st.session_state[f'uploaded_file_name_{session_id}'] = audio_file.name
|
|
|
64 |
st.session_state[f'generated_images_{session_id}'] = None # Reset image generation state
|
65 |
st.session_state[f'generated_video_{session_id}'] = None # Reset generated video state
|
66 |
st.session_state[f'video_generated_{session_id}'] = False # Reset video generated flag
|
67 |
+
logger.info("State variables reset due to new audio file upload.")
|
|
|
68 |
|
69 |
# Read the uploaded file's bytes and send to Groq API for transcription
|
70 |
file_bytes = audio_file.read()
|
71 |
+
logger.debug("Audio file bytes read successfully.")
|
72 |
|
73 |
# Create a transcription of the audio file using Groq API
|
74 |
+
try:
|
75 |
+
result = client.audio.transcriptions.create(
|
76 |
+
file=(audio_file.name, file_bytes), # Send the audio file content directly to the API
|
77 |
+
model="whisper-large-v3-turbo", # Model to use for transcription
|
78 |
+
prompt="Take Note of Overall Context of the Audio", # Optional context for better transcription accuracy
|
79 |
+
response_format="verbose_json", # Return detailed JSON response
|
80 |
+
temperature=0.0, # Control randomness in the transcription output
|
81 |
+
)
|
82 |
+
st.session_state[f'transcript_{session_id}'] = result.text
|
83 |
+
st.session_state[f'segments_{session_id}'] = result.segments
|
84 |
+
logger.info("Transcription created successfully.")
|
85 |
+
except Exception as e:
|
86 |
+
logger.error(f"Error during transcription: {e}")
|
87 |
+
st.error("An error occurred during transcription.")
|
88 |
|
89 |
# Translation logic
|
90 |
if st.session_state[f'transcript_{session_id}'] and st.session_state[f'translation_{session_id}'] is None:
|
91 |
with st.spinner("Generating translation... Please wait."):
|
92 |
st.session_state[f'translation_{session_id}'] = get_translation(st.session_state[f'transcript_{session_id}'])
|
93 |
+
logger.info("Translation generated successfully.")
|
94 |
|
95 |
st.audio(st.session_state[f'audio_{session_id}'], format=f"audio/{audio_file.type}")
|
96 |
|
|
|
118 |
st.session_state[f'image_prompts_{session_id}'] = get_image_prompts(segments_to_chunks(st.session_state[f'segments_{session_id}']))['image_prompts']
|
119 |
else:
|
120 |
st.session_state[f'image_prompts_{session_id}'] = get_image_prompts(segments_to_chunks(st.session_state[f'segments_{session_id}']))['image_prompts']
|
121 |
+
logger.info("Image prompts generated successfully.")
|
122 |
|
|
|
123 |
# Ensure that generated_images is always a list
|
124 |
if f'generated_images_{session_id}' not in st.session_state or st.session_state[f'generated_images_{session_id}'] is None:
|
125 |
st.session_state[f'generated_images_{session_id}'] = []
|
|
|
139 |
|
140 |
progress_placeholder.text("✅ All images generated successfully!")
|
141 |
progress_bar.empty()
|
142 |
+
logger.info("All images generated successfully.")
|
143 |
|
144 |
# Generate video when all images are generated
|
145 |
if st.session_state[f'generated_images_{session_id}'] and st.session_state[f'audio_{session_id}'] and not st.session_state[f'video_generated_{session_id}']:
|
|
|
159 |
st.session_state[f'generated_video_{session_id}'] = generated_video_path
|
160 |
st.session_state[f'video_generated_{session_id}'] = True # Set the flag to True
|
161 |
st.success("Video generated successfully!")
|
162 |
+
logger.info("Video generated successfully.")
|
163 |
|
164 |
# Display the generated video
|
165 |
if st.session_state[f'generated_video_{session_id}']:
|
|
|
176 |
|
177 |
else:
|
178 |
st.warning("Please upload an audio file to proceed.")
|
179 |
+
logger.warning("No audio file uploaded.")
|
180 |
|
181 |
|
utils.py
CHANGED
@@ -71,8 +71,32 @@ def get_image_prompts(text_input : List):
|
|
71 |
extractor = StructuredOutputExtractor(response_schema=ImagePromptResponseSchema)
|
72 |
chunks_count = len(text_input)
|
73 |
chunks = "chunk: " + "\nchunk: ".join(text_input)
|
74 |
-
prompt = f"""ROLE: You are a Highly Experienced Image Prompt Sythesizer
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
result = extractor.extract(prompt)
|
77 |
return result.model_dump() # returns dictionary version pydantic model
|
78 |
|
|
|
71 |
extractor = StructuredOutputExtractor(response_schema=ImagePromptResponseSchema)
|
72 |
chunks_count = len(text_input)
|
73 |
chunks = "chunk: " + "\nchunk: ".join(text_input)
|
74 |
+
prompt = f"""ROLE: You are a Highly Experienced Image Prompt Sythesizer
|
75 |
+
|
76 |
+
SYSTEM PROMPT:
|
77 |
+
|
78 |
+
1. **Combine all chunks** to understand the complete context.
|
79 |
+
2. **Identify the theme** and setting of the combined context.
|
80 |
+
3. For each chunk, **generate a simple, context-aware image prompt** that fits the overall picture.
|
81 |
+
- Keep it clear and vivid, adding small details to enhance the visual.
|
82 |
+
|
83 |
+
|
84 |
+
### Example
|
85 |
+
|
86 |
+
**Chunks**:
|
87 |
+
1. A guy went to the jungle.
|
88 |
+
2. He saw a lion.
|
89 |
+
|
90 |
+
**Combined Context**:
|
91 |
+
"A man ventured into a jungle and encountered a lion."
|
92 |
+
|
93 |
+
**Prompts**:
|
94 |
+
- **Chunk 1**: "A man walking into a dense, green jungle, with tall trees and sunlight filtering through the leaves."
|
95 |
+
- **Chunk 2**: "In a jungle clearing, a lion stands majestically, its golden mane glowing in the soft sunlight as it watches the man silently."
|
96 |
+
|
97 |
+
NOTE: Never write a prompt that can generate NSFW images, or any other explicit content, use safe and appropriate prompts
|
98 |
+
|
99 |
+
TASK: Generate {chunks_count} image prompts, Each per chunk\n\n {chunks}"""
|
100 |
result = extractor.extract(prompt)
|
101 |
return result.model_dump() # returns dictionary version pydantic model
|
102 |
|