Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,85 @@
|
|
| 1 |
-
# This is a simple script generator that uses OpenAI to generate scripts based on a document summary and content outline.
|
| 2 |
-
# It's a Streamlit app that allows the user to input the document summary, content outline, and system prompts for context and script generation.
|
| 3 |
-
# The app generates a context that captures the essential information from the summary, and then generates a detailed script for each heading in the content outline.
|
| 4 |
-
# The scripts are then displayed in the app.
|
| 5 |
-
|
| 6 |
import os
|
| 7 |
import asyncio
|
| 8 |
import streamlit as st
|
| 9 |
-
import
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"messages": [
|
| 25 |
-
{"role": "system", "content": "You are an AI assistant. Based on the document summary below, please provide a concise context that captures the essential information:"},
|
| 26 |
-
{"role": "user", "content": prompt}
|
| 27 |
-
],
|
| 28 |
-
"temperature": 0.7,
|
| 29 |
-
"max_tokens": 2048,
|
| 30 |
-
}
|
| 31 |
-
headers = {"Authorization": f"Bearer {API_KEY}"}
|
| 32 |
-
try:
|
| 33 |
-
async with session.post(API_URL, json=payload, headers=headers) as response:
|
| 34 |
-
if response.status != 200:
|
| 35 |
-
raise Exception(f"Error generating context: {response.status} - {await response.text()}")
|
| 36 |
-
result = await response.json()
|
| 37 |
-
return result["choices"][0]["message"]["content"].strip()
|
| 38 |
-
except Exception as e:
|
| 39 |
-
st.error(f"Error generating context: {str(e)}")
|
| 40 |
-
return None
|
| 41 |
|
| 42 |
async def generate_script(context, heading, system_prompt_prefix, system_prompt_suffix):
|
| 43 |
-
prompt = f"{system_prompt_prefix}\n\n{context}\n\
|
| 44 |
-
|
| 45 |
-
async with aiohttp.ClientSession() as session:
|
| 46 |
-
payload = {
|
| 47 |
-
"model": "gpt-4-turbo-preview",
|
| 48 |
-
"messages": [
|
| 49 |
-
{"role": "system", "content": f"You are an AI scriptwriter. Your task is to generate a detailed script based on the given context and the provided heading."},
|
| 50 |
-
{"role": "user", "content": prompt}
|
| 51 |
-
],
|
| 52 |
-
"temperature": 0.7,
|
| 53 |
-
"max_tokens": 4000,
|
| 54 |
-
}
|
| 55 |
-
headers = {"Authorization": f"Bearer {API_KEY}"}
|
| 56 |
-
try:
|
| 57 |
-
async with session.post(API_URL, json=payload, headers=headers) as response:
|
| 58 |
-
if response.status != 200:
|
| 59 |
-
raise Exception(f"Error generating script: {response.status} - {await response.text()}")
|
| 60 |
-
result = await response.json()
|
| 61 |
-
return result["choices"][0]["message"]["content"].strip()
|
| 62 |
-
except Exception as e:
|
| 63 |
-
st.error(f"Error generating script: {str(e)}")
|
| 64 |
-
return None
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
async def main():
|
| 69 |
st.title("Script Generator")
|
| 70 |
|
| 71 |
-
context_system_prompt_prefix = st.text_input(
|
|
|
|
|
|
|
|
|
|
| 72 |
summaries = st.text_area("Enter the document summary:", height=200)
|
| 73 |
-
context_system_prompt_suffix = st.text_input(
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
outline = st.text_area("Enter the content outline (one heading per line):", height=200)
|
| 76 |
-
script_system_prompt_prefix = st.text_input(
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
if st.button("Generate Context and Scripts"):
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
st.subheader(heading)
|
| 93 |
-
st.write(script)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
import streamlit as st
|
| 4 |
+
from openai import AsyncOpenAI
|
| 5 |
|
| 6 |
+
# OpenAI API key setup
|
| 7 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
| 8 |
+
if not api_key:
|
| 9 |
+
st.error("OpenAI API key not found. Please set the OPENAI_API_KEY environment variable.")
|
| 10 |
+
st.stop()
|
| 11 |
|
| 12 |
+
client = AsyncOpenAI(api_key=api_key)
|
| 13 |
|
| 14 |
+
async def generate_content(prompt, model="gpt-4-turbo-preview", max_tokens=2048):
|
| 15 |
+
try:
|
| 16 |
+
response = await client.chat.completions.create(
|
| 17 |
+
model=model,
|
| 18 |
+
messages=[{"role": "user", "content": prompt}],
|
| 19 |
+
max_tokens=max_tokens
|
| 20 |
+
)
|
| 21 |
+
return response.choices[0].message.content.strip()
|
| 22 |
+
except Exception as e:
|
| 23 |
+
st.error(f"Error generating content: {str(e)}")
|
| 24 |
+
return None
|
| 25 |
|
| 26 |
+
async def generate_context(summaries, system_prompt_prefix, system_prompt_suffix):
|
| 27 |
+
prompt = f"{system_prompt_prefix}\n\n{summaries}\n\n{system_prompt_suffix}"
|
| 28 |
+
return await generate_content(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
async def generate_script(context, heading, system_prompt_prefix, system_prompt_suffix):
|
| 31 |
+
prompt = f"{system_prompt_prefix}\n\n{context}\n\nHeading: {heading}\n\n{system_prompt_suffix}"
|
| 32 |
+
return await generate_content(prompt, max_tokens=4000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
def main():
|
|
|
|
|
|
|
| 35 |
st.title("Script Generator")
|
| 36 |
|
| 37 |
+
context_system_prompt_prefix = st.text_input(
|
| 38 |
+
"Enter the prompt prefix for context generation:",
|
| 39 |
+
value="You are an AI assistant. Your task is to provide a concise context that captures the essential information from the given document summary."
|
| 40 |
+
)
|
| 41 |
summaries = st.text_area("Enter the document summary:", height=200)
|
| 42 |
+
context_system_prompt_suffix = st.text_input(
|
| 43 |
+
"Enter the prompt suffix for context generation:",
|
| 44 |
+
value="Focus on the key points and main ideas, and present the context in a clear and organized manner."
|
| 45 |
+
)
|
| 46 |
|
| 47 |
outline = st.text_area("Enter the content outline (one heading per line):", height=200)
|
| 48 |
+
script_system_prompt_prefix = st.text_input(
|
| 49 |
+
"Enter the system prompt prefix for script generation:",
|
| 50 |
+
value="You are an AI scriptwriter. Your task is to generate a detailed script based on the given context and the provided heading."
|
| 51 |
+
)
|
| 52 |
+
script_system_prompt_suffix = st.text_input(
|
| 53 |
+
"Enter the system prompt suffix for script generation:",
|
| 54 |
+
value="The script should be relevant, informative, engaging, and contain more than 600 characters."
|
| 55 |
+
)
|
| 56 |
|
| 57 |
if st.button("Generate Context and Scripts"):
|
| 58 |
+
if not summaries or not outline:
|
| 59 |
+
st.error("Please enter both the document summary and content outline.")
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
+
context = asyncio.run(generate_context(summaries, context_system_prompt_prefix, context_system_prompt_suffix))
|
| 63 |
+
if context:
|
| 64 |
+
st.header("Generated Context")
|
| 65 |
+
st.write(context)
|
| 66 |
+
|
| 67 |
+
headings = [h.strip() for h in outline.split("\n") if h.strip()]
|
| 68 |
+
scripts = []
|
| 69 |
+
for heading in headings:
|
| 70 |
+
script = asyncio.run(generate_script(context, heading, script_system_prompt_prefix, script_system_prompt_suffix))
|
| 71 |
+
if script:
|
| 72 |
+
scripts.append((heading, script))
|
| 73 |
|
| 74 |
+
if scripts:
|
| 75 |
+
st.header("Generated Scripts")
|
| 76 |
+
for heading, script in scripts:
|
| 77 |
+
st.subheader(heading)
|
| 78 |
+
st.write(script)
|
| 79 |
+
else:
|
| 80 |
+
st.error("Failed to generate scripts. Please try again.")
|
| 81 |
+
else:
|
| 82 |
+
st.error("Failed to generate context. Please try again.")
|
| 83 |
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
main()
|
|
|
|
|
|