File size: 4,433 Bytes
45bfcc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# blogger.py
import os
import pyperclip
import streamlit as st
from dotenv import load_dotenv
import speech_recognition as sr
import google.generativeai as genai
from langchain.prompts import PromptTemplate
from langchain_google_genai import ChatGoogleGenerativeAI

class Blog(object):
    def __init__(self, title="AI Blogger"):
        self.title = title

    @staticmethod
    def model():
        genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
        return ChatGoogleGenerativeAI(model="gemini-pro")

    @staticmethod
    def getLLamaresponse(llm, input_text, tone, lang, blog_style, no_words, doc='', info=''):
        template = """
            Write a {tone} blog in {lang} as {blog_style} for a topic {input_text} within {no_words} words with references and video links also if possible and ends with a conclusion. {info} {doc}
                """.format(tone=tone, lang=lang, blog_style=blog_style, input_text=input_text,
                                        no_words=no_words,doc=doc, info=info)
        response = llm.invoke(template)
        return response.content

    @staticmethod
    def copy_text(answer, copy_button=False):
        pyperclip.copy(answer)
        if copy_button:
            st.toast("Text copied to clipboard!", icon="πŸ“‹")

    @staticmethod
    def record_audio():
        r = sr.Recognizer()
        with st.spinner("Recording..."):
            with sr.Microphone() as source:
                r.adjust_for_ambient_noise(source)
                with st.spinner("Say Something..."):
                    audio = r.listen(source, timeout=5)
            with st.spinner("Processing..."):
                try:
                    text = r.recognize_google(audio)
                    st.session_state['input_text'] = text
                    return text
                except sr.UnknownValueError:
                    st.write("Sorry, I could not understand what you said. Please try again or write in text box.")
                    return ""
                except sr.RequestError as e:
                    st.write(f"Could not request results; {e}")
                    return ""

    @staticmethod
    def input_state(input_text):
        if isinstance(input_text, str):
            st.session_state['input_text'] = input_text

def run_blogger(doc=''):
    load_dotenv()
    blog = Blog()

    st.header(blog.title + " πŸ€–", divider='rainbow')

    hide_streamlit_style = """
    <style>
    #MainMenu {visibility: hidden;}
    footer {visibility: hidden;}
    </style>
    """
    st.markdown(hide_streamlit_style, unsafe_allow_html=True)

    button_disabled = True

    if button_disabled:
        st.toast("Mic Button is disabled in production. HF Don't have Audio Device")

    col1, col2 = st.columns([20, 3])

    with col1:
        text = st.empty()
        input_text = text.text_input("Enter the Blog Topic")

    with col2:
        recorder = st.button("πŸŽ™οΈ", help="Blog Topic by Voice", key="mic", disabled=button_disabled)

    if recorder:
        recorded_text = blog.record_audio()
        if recorded_text:
            input_text = text.text_input("Enter the Blog Topic", value=recorded_text)

    if doc:
        input_text = text.text_area("Enter the Blog Topic", value=doc)
        input_text = "Reference: "+ input_text

    with st.sidebar:
        st.title(' :blue[_AI Generated Blog_] πŸ€–')
        st.subheader('Parameters')
        no_words = st.sidebar.slider('Maximum Characters', min_value=100, max_value=5000, value=1000, step=100)
        blog_style = st.selectbox('Writing the blog for', ('Researchers', 'Data Scientist', 'Common People'), index=0)
        tone = st.selectbox('Desired Tone', ('Informative', 'Casual', 'Persuasive', 'Formal', 'Humorous'), index=0)
        lang = st.text_input('Language', 'English')
        info = st.text_area('Specific Instruction')
        with st.spinner("Loading Model..."):
            llm = blog.model()

    submit = st.button("Generate Blog", on_click=lambda: blog.input_state(input_text))

    if submit:
        with st.spinner("Generating Blog..."):
            answer = blog.getLLamaresponse(llm, st.session_state['input_text'], tone, lang, blog_style, no_words, doc, info)
            st.success('Blog Generated!', icon="βœ…")
            st.write(answer)
            st.markdown(''':orange[Run in your system to access Copy to Clipboard Feature]''')

if __name__ == "__main__":
    run_blogger()