Spaces:
Sleeping
Sleeping
Raj Jayendrakumar Muchhala
commited on
Commit
·
2977b17
1
Parent(s):
4e88dff
update to streaming
Browse files- app.py +25 -27
- prompts.py +6 -1
app.py
CHANGED
|
@@ -52,7 +52,7 @@ if generate_button:
|
|
| 52 |
generated_plan_container.empty()
|
| 53 |
|
| 54 |
# Stream OpenAI API Response
|
| 55 |
-
|
| 56 |
model=MODEL,
|
| 57 |
messages=messages,
|
| 58 |
temperature=0.45,
|
|
@@ -60,32 +60,30 @@ if generate_button:
|
|
| 60 |
top_p=1,
|
| 61 |
frequency_penalty=0,
|
| 62 |
presence_penalty=0,
|
| 63 |
-
|
| 64 |
)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
st.write(f"**Duration:** {clip.get('Duration Target', 'N/A')} seconds")
|
| 82 |
-
st.write(f"**Aspect Ratio:** {clip.get('Aspect Ratio', 'N/A')}")
|
| 83 |
-
st.markdown("---")
|
| 84 |
-
else:
|
| 85 |
-
st.error("⚠️ No clips were generated. Try again.")
|
| 86 |
-
|
| 87 |
-
except json.JSONDecodeError:
|
| 88 |
-
st.error("⚠️ Failed to parse OpenAI response. Try again.")
|
| 89 |
-
except Exception as e:
|
| 90 |
-
st.error(f"❌ Error: {str(e)}")
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
generated_plan_container.empty()
|
| 53 |
|
| 54 |
# Stream OpenAI API Response
|
| 55 |
+
response_stream = client.chat.completions.create(
|
| 56 |
model=MODEL,
|
| 57 |
messages=messages,
|
| 58 |
temperature=0.45,
|
|
|
|
| 60 |
top_p=1,
|
| 61 |
frequency_penalty=0,
|
| 62 |
presence_penalty=0,
|
| 63 |
+
stream=True,
|
| 64 |
)
|
| 65 |
|
| 66 |
+
# Process streamed response line-by-line
|
| 67 |
+
buffer = "" # Stores partial text before a newline
|
| 68 |
+
output_text = ""
|
| 69 |
+
|
| 70 |
+
for chunk in response_stream:
|
| 71 |
+
if chunk.choices:
|
| 72 |
+
buffer += chunk.choices[0].delta.content or "" # Append new text
|
| 73 |
+
|
| 74 |
+
# Check if we received a complete line
|
| 75 |
+
while "\n" in buffer:
|
| 76 |
+
line, buffer = buffer.split("\n", 1)
|
| 77 |
+
line = line.strip()
|
| 78 |
+
|
| 79 |
+
if not line:
|
| 80 |
+
continue # Ignore empty lines
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# Append new line to output
|
| 83 |
+
output_text += f"{line}\n"
|
| 84 |
+
|
| 85 |
+
# Display dynamically
|
| 86 |
+
generated_plan_container.markdown(f"```\n{output_text}\n```")
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
st.error(f"❌ Error: {str(e)}")
|
prompts.py
CHANGED
|
@@ -15,7 +15,12 @@ Your goal is to analyze the provided transcript and generate a **high-level plan
|
|
| 15 |
- **Duration Target:** Estimated duration (in seconds) based on the transcript length and complexity.
|
| 16 |
- **Aspect Ratio:** Recommended format (Landscape, Square, Vertical) based on content type.
|
| 17 |
|
| 18 |
-
Format the response as
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
'''
|
| 20 |
|
| 21 |
USER_MESSAGE = '''
|
|
|
|
| 15 |
- **Duration Target:** Estimated duration (in seconds) based on the transcript length and complexity.
|
| 16 |
- **Aspect Ratio:** Recommended format (Landscape, Square, Vertical) based on content type.
|
| 17 |
|
| 18 |
+
Format the response as:
|
| 19 |
+
Clip 1
|
| 20 |
+
- Title: <Title>
|
| 21 |
+
- Focus Prompt: <Focus Prompt>
|
| 22 |
+
- Duration Target: <Duration Target> seconds
|
| 23 |
+
- Aspect Ratio: <Aspect Ratio>
|
| 24 |
'''
|
| 25 |
|
| 26 |
USER_MESSAGE = '''
|