Spaces:
Sleeping
Sleeping
xuyingliKepler
commited on
Commit
•
a3f5f57
1
Parent(s):
282119b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import random
|
3 |
+
import openai
|
4 |
+
import pandas as pd
|
5 |
+
import os
|
6 |
+
|
7 |
+
st.set_page_config(layout="wide")
|
8 |
+
st.balloons()
|
9 |
+
|
10 |
+
|
11 |
+
os.environ["OPENAI_API_KEY"]= st.secrets["SERPER_API_KEY"]
|
12 |
+
openai.api_key = os.environ["OPENAI_API_KEY"]
|
13 |
+
|
14 |
+
if "df" not in st.session_state:
|
15 |
+
st.session_state.df = pd.read_csv('matt.csv')
|
16 |
+
|
17 |
+
if "type" not in st.session_state:
|
18 |
+
st.session_state.type = ''
|
19 |
+
|
20 |
+
if "time" not in st.session_state:
|
21 |
+
st.session_state.time = ''
|
22 |
+
|
23 |
+
if "check" not in st.session_state:
|
24 |
+
st.session_state.check = False
|
25 |
+
|
26 |
+
if "option" not in st.session_state:
|
27 |
+
st.session_state.option = ''
|
28 |
+
|
29 |
+
if "result" not in st.session_state:
|
30 |
+
st.session_state.result = []
|
31 |
+
|
32 |
+
if 'num_textareas' not in st.session_state:
|
33 |
+
# 用来计数textarea的数量
|
34 |
+
st.session_state['num_textareas'] = 1
|
35 |
+
|
36 |
+
def add_text_area():
|
37 |
+
if st.session_state['num_textareas'] < 5:
|
38 |
+
st.session_state['num_textareas'] += 1
|
39 |
+
|
40 |
+
def get_completion_from_messages(messages,
|
41 |
+
model="gpt-3.5-turbo-16k",
|
42 |
+
temperature=1.5, max_tokens=3000):
|
43 |
+
response = openai.ChatCompletion.create(
|
44 |
+
model=model,
|
45 |
+
messages=messages,
|
46 |
+
temperature=temperature,
|
47 |
+
max_tokens=max_tokens,
|
48 |
+
)
|
49 |
+
return response.choices[0].message["content"]
|
50 |
+
|
51 |
+
def sample_generation(query, option = st.session_state.option, time = st.session_state.time, df = st.session_state.df):
|
52 |
+
samples = df[option]
|
53 |
+
print(samples)
|
54 |
+
system_message = f'''
|
55 |
+
You are an excellent press release writer;
|
56 |
+
you craft narratives from a first-person perspective.
|
57 |
+
You can reference the content and style of the examples
|
58 |
+
to rewrite user input text in 【 】
|
59 |
+
in the same style. Elaborate when necessary.
|
60 |
+
Examples: {samples}
|
61 |
+
'''
|
62 |
+
|
63 |
+
messages = [
|
64 |
+
{'role':'system',
|
65 |
+
'content': system_message + "keep it equal to {} words.".format(int(time)*60)},
|
66 |
+
{'role':'user',
|
67 |
+
'content': f"【{query}】"},]
|
68 |
+
|
69 |
+
res = get_completion_from_messages(messages)
|
70 |
+
return res
|
71 |
+
|
72 |
+
st.title('script generator')
|
73 |
+
tab1, tab2 = st.tabs(["Generation", "Library"])
|
74 |
+
|
75 |
+
with tab1:
|
76 |
+
col1, col2 = st.columns([2, 3])
|
77 |
+
with col1:
|
78 |
+
st.session_state.option = st.selectbox(
|
79 |
+
'',
|
80 |
+
("Intro", "News description", "Connection", 'Sponser', 'End of the video'))
|
81 |
+
for i in range(st.session_state['num_textareas']):
|
82 |
+
st.text_area(f"points {i+1}", key=f"textarea{i+1}")
|
83 |
+
st.button("➕", on_click=add_text_area)
|
84 |
+
st.session_state.time = st.slider("time mins", 0, 20, 5)
|
85 |
+
if st.button('Generate'):
|
86 |
+
# 可以在这里处理所有textarea的数据
|
87 |
+
textarea_value = ''
|
88 |
+
for i in range(st.session_state['num_textareas']):
|
89 |
+
# 使用st.session_state获取特定的textarea输入值
|
90 |
+
textarea_value += st.session_state.get(f"textarea{i+1}")
|
91 |
+
|
92 |
+
progress_text = "Operation in progress. Please wait."
|
93 |
+
my_bar = st.progress(0, text=progress_text)
|
94 |
+
|
95 |
+
for i in range(3):
|
96 |
+
my_bar.progress((i+1) * 30, text=progress_text)
|
97 |
+
res = sample_generation(textarea_value)
|
98 |
+
st.session_state.result.append(res)
|
99 |
+
st.session_state.check = True
|
100 |
+
my_bar.empty()
|
101 |
+
|
102 |
+
with col2:
|
103 |
+
if st.session_state.check:
|
104 |
+
for i in range(len(st.session_state.result)):
|
105 |
+
st.text_area("Sample" + str(i+1), value=st.session_state.result[i], height=300, key="text_area_"+str(i))
|
106 |
+
|
107 |
+
else: st.image("https://static.streamlit.io/examples/dog.jpg")
|
108 |
+
|
109 |
+
with tab2:
|
110 |
+
tab3, tab4, tab5, tab6, tab7 = st.tabs(["Intro", "News description", "Connection", 'Sponser', 'End of the video'])
|
111 |
+
with tab3:
|
112 |
+
st.table(st.session_state.df['Intro'])
|
113 |
+
with tab4:
|
114 |
+
st.table(st.session_state.df['News description'])
|
115 |
+
with tab5:
|
116 |
+
st.table(st.session_state.df['Connection'])
|
117 |
+
with tab6:
|
118 |
+
st.table(st.session_state.df['Sponser'])
|
119 |
+
with tab7:
|
120 |
+
st.table(st.session_state.df['End of the video'])
|