File size: 8,764 Bytes
46290fc
 
 
2c20470
 
11dec1a
457e1fe
2c20470
 
457e1fe
46290fc
 
 
2c20470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9081a1
2c20470
 
 
 
 
 
 
 
 
 
 
e9081a1
2c20470
 
89a1b78
8380f70
2c20470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46290fc
 
 
 
 
 
 
 
2c20470
46290fc
 
 
 
 
 
 
 
2c20470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457e1fe
2c20470
 
 
46290fc
2c20470
 
 
46290fc
 
2c20470
 
 
 
 
 
 
 
 
 
 
e9081a1
8380f70
2c20470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46290fc
2c20470
 
 
 
46290fc
 
 
 
 
 
 
2c20470
 
46290fc
 
 
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
import streamlit as st 
import re
import openai
from paraphrase_post import get_original_url , paraphrased_post 
from advance_post import google_search 
from langchain_community.chat_models import ChatOpenAI
from langchain_cohere import ChatCohere
from streaming_response import StreamHandler
from blog_post import post_from_content
# from langchain_nvidia_ai_endpoints import ChatNVIDIA
#from langchain import HuggingFaceHub


# def main():
st.title("LinkedIn Post Creator")

# Initialize SessionState dictionary
session_state = st.session_state

if 'paraphrase' not in session_state:
    session_state.paraphrase = ""
if 'keywords' not in session_state:
    session_state.keywords = ""
if 'take_aways' not in session_state:
    session_state.take_aways = ""
if 'highlights' not in session_state:
    session_state.highlights = ""

if 'advancepost' not in session_state:
    session_state.advancepost = ""

# url = st.sidebar.text_input("Enter URL:", placeholder="Enter URL here...")
option = st.sidebar.selectbox('Select Model:', ('GPT-4o',"Cohere"))
temperature= st.sidebar.select_slider(
'How much accurate post you want ?',
options=['Less accuracy', 9, 8, 7, 6, 5,4,3 ,2,1,'High accuracy'])
if temperature=='Less accuracy':
    temperature=10
elif temperature=="High accuracy":
    temperature=0
temperature=temperature/10

# stream_handler = StreamHandler(st.empty())  

if option=="GPT-4o":
    api_key=st.sidebar.text_input("API Key:",placeholder="Enter OpenAI API Key...")
    if api_key:
        stream_handler = StreamHandler(st.empty())
        model=ChatOpenAI(model="gpt-4o" , temperature=temperature , api_key=api_key , streaming=True, callbacks=[stream_handler])
elif option=="Cohere":
    api_key= st.sidebar.text_input("API Key:",placeholder="Enter Cohere API Key...")
    if api_key:
        stream_handler = StreamHandler(st.empty())    
        model = ChatCohere(cohere_api_key=api_key,temperature=temperature ,  streaming=True, callbacks=[stream_handler])
        #model = ChatCohere(cohere_api_key=api_key , temperature = temperature , model = "command-r-plus")
# elif option=="Mixtral-8x7b":
#     api_key=st.sidebar.text_input("API Key:",placeholder="Enter NVIDIA NIM API Key...")
#     if api_key:
#         model = ChatNVIDIA(model="mixtral_8x7b",api_key = api_key , temperature=temperature)




# elif option=="Llama-3":
#     api_key=st.sidebar.text_input("API Key:",placeholder="Enter HuggingFace API Token...")
#     if api_key:
#         model=HuggingFaceHub(repo_id="mistralai/Mixtral-8x22B-Instruct-v0.1",huggingfacehub_api_token=api_key ,model_kwargs={"temperature":temperature})
        


if st.sidebar.toggle("Normal LinkedIn Post"):        
    url = st.sidebar.text_input("Enter URL:", placeholder="Enter URL here...")   
    if st.sidebar.button("Submit"):
        if url:
            if api_key:
                original_url = get_original_url(url)
                match = re.match(r"https?://(?:www\.)?linkedin\.com/(posts|feed|pulse)/.*", original_url)  # checking domain and url page (means it should only be a post nothing else like login page or something else)

                if match:
                    try:
                        session_state.paraphrase = paraphrased_post(url , model) 
                    except (openai.AuthenticationError) as e:
                        st.sidebar.error("Enter your valid API key")
                else:
                    st.sidebar.error("Put a valid LinkedIn post url only")
            else:
                st.sidebar.error("Please enter API Key")
        else:
            st.sidebar.error("Please enter url")
    from paraphrase_post import generate_details
    if st.sidebar.button("Show Details"):
        session_state.keywords =generate_details(session_state.paraphrase , model) 



# paraphrase_text=st.text_area("Generated LinkedIn post",value=session_state.paraphrase, height=400)
# import pyperclip
# if st.button('Copy'):
#     pyperclip.copy(paraphrase_text)
#     st.success('Text copied successfully!')
# from paraphrase_post import extract_data
# # session_state.keywords , session_state.take_aways , session_state.highlights =extract_data(session_state.paraphrase , model) 
# from paraphrase_post import generate_details
# if st.sidebar.button("Show Details"):
#     session_state.keywords =generate_details(session_state.paraphrase , model)   
        # session_state.take_aways =generate_takeaways(session_state.paraphrase , model)
        # session_state.highlights =generate_highlights(session_state.paraphrase , model) 
        
        # st.write("Keywords:")
        # for i, statement in enumerate(session_state.keywords, start=1):
        #     st.write(f"{i}. {statement}")
    
        # st.write("Take Aways:")
        # for i, statement in enumerate(session_state.take_aways, start=1):
        #     st.write(f"{i}. {statement}")

        # st.write("Highlights:")
        # for i, statement in enumerate(session_state.highlights, start=1):
        #     st.write(f"{i}. {statement}")

#------------------------------------------------------------Advance LinkedIn post code below-----------------------------------------------------------------


if st.sidebar.toggle("Advance LinkedIn Post"):
    url = st.sidebar.text_input("Enter URL", placeholder="Enter URL here...")   
    google_api_key=st.sidebar.text_input("Google API Key:",placeholder="Enter Google Search API Key...")
    search_engine_id=st.sidebar.text_input("Search Engine ID:",placeholder="Enter Search Engine ID...")
    google_api_key = "AIzaSyABoOsfUbydc1AfVf2Zjto4MFWNgjLSJmY"
    search_engine_id = "243a392ba3dff442c"
    if st.sidebar.button("Generate Advance Post"):
        if url:
            if api_key:
                if option=="GPT-4o":
                        model=ChatOpenAI(model="gpt-4o" , temperature=temperature , api_key=api_key)
                elif option=="Cohere":
                        model = ChatCohere(cohere_api_key=api_key,temperature=temperature)
                        if google_api_key:
                            if search_engine_id:
                                original_url = get_original_url(url)
                                match = re.match(r"https?://(?:www\.)?linkedin\.com/(posts|feed|pulse)/.*", original_url)  # checking domain and url page (means it should only be a post nothing else like login page or something else)

                                if match:
                                    try:
                                        session_state.advancepost =google_search(url ,model , google_api_key,search_engine_id)
                                        # session_state.advancepost = advanced_post(all_links ,model ,session_state.paraphrase)
                                    except (openai.AuthenticationError) as e:
                                        st.sidebar.error("Enter your valid API key")
                                else:
                                    st.sidebar.error("Put a valid LinkedIn post url only")
                                    # if len(docs)==0:
                                #     st.sidebar.error("Please Check your both credentials carefully")
                    
                            else:
                                st.sidebar.error("Please enter Search Engine ID")
                        else:
                            st.sidebar.error("Please enter Google API Key")
            else:
                st.sidebar.error("Please enter API key")
        else:
            st.sidebar.error("Please enter url")

    advance_post=st.text_area("Advance LinkedIn post",value=session_state.advancepost, height=400) 




# from streaming_response import StreamHandler
if st.sidebar.toggle("Message"):
    content=st.sidebar.text_area("Paste your content" ,height=300)
    if st.sidebar.button("Generate Post"):
        if content:
            if api_key:
                # with st.container(height=500):
                    # stream_handler = StreamHandler(st.empty())    
                    # model = ChatCohere(cohere_api_key=api_key,temperature=temperature ,  streaming=True, callbacks=[stream_handler])
                    post_from_content(model ,content)
                    
                
            else:
                st.sidebar.error("Please enter API Key")
        else:
            st.sidebar.error("You have not provided any content in Textbox")
    
            
    
        # if st.button('Copy Advanced Post'):
        #     pyperclip.copy(advance_post)
        #     st.success('Text copied successfully!')
#--------------------------------------------------------------------------------------------------------------------------------------------------------------

# if __name__ == "__main__":
#     main()