Raj Jayendrakumar Muchhala commited on
Commit
2977b17
·
1 Parent(s): 4e88dff

update to streaming

Browse files
Files changed (2) hide show
  1. app.py +25 -27
  2. 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
- response = client.chat.completions.create(
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
- response_format={"type": "json_object"},
64
  )
65
 
66
- # Parse the response
67
- generated_response = response.choices[0].message.content.strip()
68
- content_plan = json.loads(generated_response)
69
-
70
- # Extract key (assuming there is only one key in the JSON response)
71
- plan_key = list(content_plan.keys())[0]
72
- clip_plans = content_plan.get(plan_key, [])
73
-
74
- # Display final output
75
- if clip_plans:
76
- with generated_plan_container.container():
77
- for i, clip in enumerate(clip_plans):
78
- st.markdown(f"### 🎬 Clip {i + 1}")
79
- st.write(f"**Title:** {clip.get('Title', 'N/A')}")
80
- st.write(f"**Focus Prompt:** {clip.get('Focus Prompt', 'N/A')}")
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 a JSON object with Title, Focus Prompt, Duration Target, and Aspect Ratio for each clip concept.
 
 
 
 
 
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 = '''