File size: 9,801 Bytes
b5f6ee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
def wait():
    import streamlit as st
    import time

    progress_texts = ["Generating Code...:pencil:","Creating App...:running:","Rendering the demo page...:tv:"]
    num_of_texts = len(progress_texts)
    progress_texts_iter =  iter(progress_texts)
    my_bar = st.progress(0, "Initializing...")
    with st.spinner('Processing...'):
        start = end = 0
        for i in range(num_of_texts):
            text = next(progress_texts_iter)
            start = end
            end = start + 100 // num_of_texts
            for percent_complete in range(start, end):
                time.sleep(0.03*(num_of_texts-i))
                my_bar.progress(percent_complete + 1, text=text)
    my_bar.empty()
   
def language_translator(openai_api_key,demo_title="My Lang App"):
    import streamlit as st
    from langchain import LLMChain
    from langchain.chat_models import ChatOpenAI
    from langchain.prompts.chat import (
        ChatPromptTemplate,
        SystemMessagePromptTemplate,
        HumanMessagePromptTemplate,
    )

    def language_translator(input_language, output_language, text):
        chat = ChatOpenAI(openai_api_key=openai_api_key, temperature=0)

        template = "You are a helpful assistant that translates {input_language} to {output_language}. Please provide the text to translate."
        system_message_prompt = SystemMessagePromptTemplate.from_template(template)
        human_template = "{text}"
        human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
        chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

        chain = LLMChain(llm=chat, prompt=chat_prompt)
        result = chain.run(input_language=input_language, output_language=output_language, text=text)
        return result

    st.header(demo_title)
    
    input_language = st.text_input("Input Language")
    output_language = st.text_input("Output Language")
    text = st.text_area("Text")

    if st.button("Translate"):
        result = language_translator(input_language, output_language, text)
        st.write(result)
        st.balloons()

def blog_post_generator(openai_api_key,demo_title="My Blogger"):
    import streamlit as st
    from langchain import LLMChain
    from langchain.chat_models import ChatOpenAI
    from langchain.prompts.chat import (
        ChatPromptTemplate,
        SystemMessagePromptTemplate,
        HumanMessagePromptTemplate,
    )

    def generate_blog_post(title):
        print("Generating blog post")
        chat = ChatOpenAI(openai_api_key=openai_api_key, temperature=0)

        template = "You are a helpful assistant that generates a blog post from the title: {title}. Please provide some content."
        system_message_prompt = SystemMessagePromptTemplate.from_template(template)
        human_template = "{text}"
        human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
        chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

        chain = LLMChain(llm=chat, prompt=chat_prompt)
        result = chain.run(title=title, text="")
        return result

    st.header(demo_title)

    title = st.text_input("Enter the title of your blog post")
    if st.button("Generate Blog Post"):
        print("Generate")
        with st.spinner("Generating the blog post..."):
            result = generate_blog_post(title)
            st.write(result)
            st.balloons()

def grammer_corrector(openai_api_key,demo_title="My Grammerly"):
    import streamlit as st
    from langchain import LLMChain
    from langchain.chat_models import ChatOpenAI
    from langchain.prompts.chat import (
        ChatPromptTemplate,
        SystemMessagePromptTemplate,
        HumanMessagePromptTemplate,
    )

    def correct_grammar(text):
        chat = ChatOpenAI(openai_api_key=openai_api_key, temperature=0)

        template = "You are a helpful assistant that corrects grammar. Please provide the text you want to correct."
        system_message_prompt = SystemMessagePromptTemplate.from_template(template)
        human_template = "{text}"
        human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
        chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

        chain = LLMChain(llm=chat, prompt=chat_prompt)
        result = chain.run(text=text)
        return result

    st.header(demo_title)

    text = st.text_input("Enter the text you want to correct")
    if st.button("Correct Grammar"):
        result = correct_grammar(text)
        st.write(result)
        st.balloons()

def lyrics_generator(openai_api_key,demo_title="Lyrics Maker"):
    import streamlit as st
    from langchain import LLMChain
    from langchain.chat_models import ChatOpenAI
    from langchain.prompts.chat import (
        ChatPromptTemplate,
        SystemMessagePromptTemplate,
        HumanMessagePromptTemplate,
    )

    def generate_song(title):
        chat = ChatOpenAI(openai_api_key=openai_api_key, temperature=0)

        template = "You are a helpful assistant that generates a song from the title: {title}. Please provide some lyrics."
        system_message_prompt = SystemMessagePromptTemplate.from_template(template)
        human_template = "{text}"
        human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
        chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

        chain = LLMChain(llm=chat, prompt=chat_prompt)
        result = chain.run(title=title, text="")
        return result

    st.header(demo_title)

    title = st.text_input("Enter the song title:")
    if st.button("Generate Song"):
        with st.spinner("Generating song..."):
            result = generate_song(title)
            st.write(result)
            st.balloons()

def twit_generator(openai_api_key,demo_title="My AutoTwitter"):
    import streamlit as st
    from langchain import LLMChain
    from langchain.chat_models import ChatOpenAI
    from langchain.prompts.chat import (
        ChatPromptTemplate,
        SystemMessagePromptTemplate,
        HumanMessagePromptTemplate,
    )

    def twitter(hashtag):
        chat = ChatOpenAI(openai_api_key=openai_api_key, temperature=0.1)

        template = "You are a helpful assistant that generate twit from {hashtag}. Please provide the hashtag to generate a twit."
        system_message_prompt = SystemMessagePromptTemplate.from_template(template)
        human_template = "Only generate the corresponding twit for this hashtag {hashtag}"
        human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
        chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

        chain = LLMChain(llm=chat, prompt=chat_prompt)
        result = chain.run(hashtag=hashtag)
        return result

    st.header(demo_title)
    
    hashtag = st.text_input("Hashtag",placeholder="#")

    if st.button("Generate"):
        result = twitter(hashtag)
        st.write(result)
        st.balloons()

def email_generator(openai_api_key,demo_title="My AutoTwitter"):
    import streamlit as st
    from langchain import LLMChain
    from langchain.chat_models import ChatOpenAI
    from langchain.prompts.chat import (
        ChatPromptTemplate,
        SystemMessagePromptTemplate,
        HumanMessagePromptTemplate,
    )

    def email(sender_name,receiver_name,purpose,keywords,tone):
        chat = ChatOpenAI(openai_api_key=openai_api_key, temperature=0.1)

        template = "You are a helpful assistant that generate email to a person according to the given purpose, keywords and tone."
        system_message_prompt = SystemMessagePromptTemplate.from_template(template)
        human_template = """Generate email for a person according to the given purpose, keywords and tone.
        Sender Name:{sender_name}
        Receiver Name:{receiver_name}
        Purpose:{purpose}
        Keywords:{keywords}
        Tone:{tone}
        Directly start to type an email
        """
        human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
        chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

        chain = LLMChain(llm=chat, prompt=chat_prompt)
        result = chain.run(sender_name=sender_name, receiver_name=receiver_name, purpose=purpose, keywords=keywords, tone=tone)
        return result

    st.header(demo_title)
    
    sender_name = st.text_input("Name of the sender")
    receiver_name = st.text_input("Receiver of the sender")
    purpose = st.text_input("Purpose of email")
    keywords = st.text_input("Primary keywords",placeholder="comma separated list of keywords")
    tone = st.text_input("Tone of the email")

    if st.button("Generate"):
        with st.spinner("Generating email..."):
            result = email(sender_name,receiver_name,purpose,keywords,tone)
            st.write(result)
            st.balloons()

examples1 = [
    "Language Translator πŸ“",
    "Grammer Corrector πŸ› ",
    "Blog post generator from title πŸ“”"
    ]

examples2=[
    "Lyrics generator from song title 🎀",
    "Twit generation from hashtag 🐦",
    'Email generator :email:'
    ] 

examples = examples1 + examples2

pages1 = [language_translator,grammer_corrector,blog_post_generator]
pages2=[lyrics_generator,twit_generator,email_generator]

pages = pages1 + pages2

example2pages={
    example:page
    for example,page in zip(examples,pages)
}


__all__ = ['language_translator','grammer_corrector','blog_post_generator','lyrics_generator','twit_generator',
           'example2pages', 'examples', 'examples1', 'examples2', 'wait']