Adrien commited on
Commit
fe5c874
·
1 Parent(s): 490c490

First commit

Browse files
Files changed (2) hide show
  1. main.py +65 -0
  2. writer.py +145 -0
main.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from email import message
2
+ import streamlit as st
3
+ from writer import write_article, incorporate_feedback, _template
4
+
5
+ st.title("Linkedin shill")
6
+
7
+ #! I hate this
8
+ if "feedback_interface" not in st.session_state:
9
+ st.session_state.feedback_interface = 0
10
+
11
+ st.session_state.content = (
12
+ ":heart_eyes: **Start writing today with the power of AI** :hugging_face:"
13
+ )
14
+
15
+ with st.container(border=True):
16
+ container = st.empty()
17
+ container.markdown(st.session_state.content)
18
+
19
+ progress_text = "Operation in progress. Please wait."
20
+
21
+ with st.container():
22
+ feedback_container = st.empty()
23
+
24
+ with st.sidebar:
25
+ with st.form("my_form"):
26
+ topic = st.text_input("topic", "Oil future")
27
+ template = st.text_area("template", _template, height=600)
28
+ purpose = st.text_input("purpose", "Informative")
29
+ audience = st.text_input("audience", "Linkedin")
30
+ tone_style = st.text_input("tone style", "engaging and informative")
31
+ key_points = st.text_input(
32
+ "key points", "efficiency, climate change, energy dependance"
33
+ )
34
+ num_words = st.text_input("number of words", "600")
35
+ language = st.text_input("language", "english")
36
+ if st.form_submit_button(label="Submit"):
37
+ user_inputs = {
38
+ "topic": topic,
39
+ "template": template,
40
+ "purpose": purpose,
41
+ "audience": audience,
42
+ "tone_style": tone_style,
43
+ "key_points": key_points,
44
+ "num_words": num_words,
45
+ "language": language,
46
+ }
47
+ bar = 1
48
+ with st.status(progress_text) as status:
49
+ container.status("Sit back and relax! AI is doing the job for you!")
50
+ st.session_state.content = write_article(user_inputs)
51
+ st.session_state.feedback_interface = 1
52
+ container.markdown(st.session_state.content)
53
+ status.update(label="Writing complete!", state="complete")
54
+
55
+ if st.session_state.feedback_interface:
56
+ with st.container(border=True):
57
+ messages = st.container(height=300)
58
+ if feedback := st.chat_input("What do you want to change?"):
59
+ messages.chat_message("user").write(feedback)
60
+ container.status("Sit back and relax! AI is doing the job for you!")
61
+ st.session_state.content = incorporate_feedback(
62
+ st.session_state.content, feedback
63
+ )
64
+ messages.chat_message("ai").write("Done!")
65
+ container.markdown(st.session_state.content, height=800)
writer.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dspy
2
+ from dspy import Signature, InputField, Module, ChainOfThought, Predict
3
+ import streamlit as st
4
+
5
+ dspy.settings.configure(
6
+ lm=dspy.OpenAI(
7
+ model="gpt-4-0125-preview",
8
+ max_tokens=4096,
9
+ api_key=st.secrets["OpenAI"],
10
+ )
11
+ )
12
+
13
+
14
+ # refin_lm = dspy.OpenAI(
15
+ # model="gpt-3.5-turbo",
16
+ # max_tokens=4096,
17
+ # api_key="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",
18
+ # )
19
+
20
+ _template = """A few controversial things I believe about {Topic}:
21
+
22
+ 1) {StrongOpinion1}
23
+
24
+ I’m not saying {SubtleDistinction}.
25
+
26
+ I’m just saying {ClarifyingStatement}.
27
+
28
+ -
29
+
30
+ 2) {StrongOpinion2}
31
+
32
+ Contrary to popular belief, {ClicheAction} is a myth.
33
+
34
+ - {FailureExample1}
35
+ - {FailureExample2}
36
+ - {FailureExample3}
37
+
38
+ {ClicheAction} is a fast-track to {UndesirableOutcome}.
39
+
40
+ -
41
+
42
+ 3) {StrongOpinion3}
43
+
44
+ “Most people” do what “most people” do.
45
+
46
+ Which leads to…
47
+
48
+ - {NegativeStat1}
49
+ - {NegativeStat2}
50
+ - {NegativeStat3}
51
+
52
+ Clearly what “most people do” doesn’t work.
53
+
54
+ So do something else.
55
+
56
+ -
57
+
58
+ 4) {StrongOpinion4}
59
+
60
+ 99% of the time, this is a giant mistake.
61
+
62
+ Instead, {Name} has a great framework for achieving {Outcome} without falling into {Trap}.
63
+
64
+ - {Step1}
65
+ - {Step2}
66
+ - {Step3}
67
+ -
68
+
69
+ 5) {StrongOpinion5}
70
+
71
+ Pursuing {Outcome} is a silly goal.
72
+
73
+ Because {UnconventionalReason}.
74
+
75
+ {DifferentOutcome} is a better pursuit in life.
76
+
77
+ - {Benefit1}
78
+ - {Benefit2}
79
+ - {Benefit3}"""
80
+
81
+
82
+ class GenerateLinkedinArticle(Signature):
83
+ topic = InputField(desc="Title or brief description of content")
84
+ template = InputField(prefix="template to follow")
85
+ purpose = InputField()
86
+ audience = InputField()
87
+ tone_style = InputField()
88
+ key_points = InputField()
89
+ num_words = InputField()
90
+ language = InputField()
91
+ article = dspy.OutputField(desc=f"Linkedin article of {num_words} words")
92
+
93
+
94
+ class LinkedinArticle(Module):
95
+ def __init__(self):
96
+ super().__init__()
97
+ self.generate_article = ChainOfThought(GenerateLinkedinArticle)
98
+
99
+ def forward(
100
+ self,
101
+ topic,
102
+ template,
103
+ purpose,
104
+ audience,
105
+ tone_style,
106
+ key_points,
107
+ num_words,
108
+ language,
109
+ ):
110
+ article_prediction = self.generate_article(
111
+ topic=topic,
112
+ template=template,
113
+ purpose=purpose,
114
+ audience=audience,
115
+ tone_style=tone_style,
116
+ key_points=key_points,
117
+ num_words=num_words,
118
+ language=language,
119
+ )
120
+ return article_prediction.article
121
+
122
+
123
+ class Feedback(Module):
124
+ def __init__(self):
125
+ super().__init__()
126
+ self.feedback = ChainOfThought("original_content, feedback -> refined_content")
127
+
128
+ def forward(self, original_content, feedback):
129
+ corrected_article = self.feedback(
130
+ original_content=original_content, feedback=feedback
131
+ ).refined_content
132
+ return corrected_article
133
+
134
+
135
+ def write_article(user_inputs: dict) -> str:
136
+ article = LinkedinArticle()
137
+ content = article.forward(**user_inputs)
138
+ return content
139
+
140
+
141
+ def incorporate_feedback(original_content, feedback):
142
+ refined_content = Feedback().forward(
143
+ original_content=original_content, feedback=feedback
144
+ )
145
+ return refined_content