shuhayas commited on
Commit
1863a9b
·
verified ·
1 Parent(s): 32e09f2

Create App.py

Browse files
Files changed (1) hide show
  1. aap.py +67 -0
aap.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import asyncio
3
+ import streamlit as st
4
+ import aiohttp
5
+
6
+ API_KEY = os.environ["OPENAI_API_KEY"]
7
+ API_URL = "https://api.openai.com/v1/chat/completions"
8
+
9
+ async def generate_context(summaries, system_prompt_prefix, system_prompt_suffix):
10
+ prompt = f"{system_prompt_prefix}\n\nYou are an AI assistant. Based on the document summary below, please provide a concise context that captures the essential information:\n\n{summaries}\n\n{system_prompt_suffix}"
11
+
12
+ async with aiohttp.ClientSession() as session:
13
+ payload = {
14
+ "model": "gpt-4-turbo-preview",
15
+ "messages": [{"role": "user", "content": prompt}],
16
+ "temperature": 0,
17
+ "max_tokens": 2048,
18
+ }
19
+ headers = {"Authorization": f"Bearer {API_KEY}"}
20
+ async with session.post(API_URL, json=payload, headers=headers) as response:
21
+ result = await response.json()
22
+ return result["choices"][0]["message"]["content"].strip()
23
+
24
+ async def generate_script(context, heading, system_prompt_prefix, system_prompt_suffix):
25
+ prompt = f"{system_prompt_prefix}\n\n{context}\n\nYou are an AI scriptwriter. Based on the context above and the heading provided below, please generate a detailed script of more than 600 characters.\n\nHeading: {heading}\n\n{system_prompt_suffix}"
26
+
27
+ async with aiohttp.ClientSession() as session:
28
+ payload = {
29
+ "model": "gpt-4-turbo-preview",
30
+ "messages": [{"role": "user", "content": prompt}],
31
+ "temperature": 0,
32
+ "max_tokens": 2048,
33
+ }
34
+ headers = {"Authorization": f"Bearer {API_KEY}"}
35
+ async with session.post(API_URL, json=payload, headers=headers) as response:
36
+ result = await response.json()
37
+ return result["choices"][0]["message"]["content"].strip()
38
+
39
+ async def main():
40
+ st.title("Script Generator")
41
+
42
+ summaries = st.text_area("Enter the document summary:", height=200)
43
+ context_system_prompt_prefix = st.text_input("Enter the system prompt prefix for context generation:", value="")
44
+ context_system_prompt_suffix = st.text_input("Enter the system prompt suffix for context generation:", value="")
45
+
46
+ outline = st.text_area("Enter the content outline (one heading per line):", height=200)
47
+ script_system_prompt_prefix = st.text_input("Enter the system prompt prefix for script generation:", value="")
48
+ script_system_prompt_suffix = st.text_input("Enter the system prompt suffix for script generation:", value="")
49
+
50
+ if st.button("Generate Context and Scripts"):
51
+ with st.spinner("Generating context..."):
52
+ context = await generate_context(summaries, context_system_prompt_prefix, context_system_prompt_suffix)
53
+ st.header("Generated Context")
54
+ st.write(context)
55
+
56
+ headings = outline.split("\n")
57
+ with st.spinner("Generating scripts..."):
58
+ tasks = [generate_script(context, heading, script_system_prompt_prefix, script_system_prompt_suffix) for heading in headings]
59
+ scripts = await asyncio.gather(*tasks)
60
+
61
+ st.header("Generated Scripts")
62
+ for heading, script in zip(headings, scripts):
63
+ st.subheader(heading)
64
+ st.write(script)
65
+
66
+ if __name__ == "__main__":
67
+ asyncio.run(main())