File size: 20,963 Bytes
256d47f
 
 
faeba56
64b1440
256d47f
 
 
 
 
 
 
 
 
 
 
 
 
64b1440
 
 
 
 
 
 
 
256d47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b1440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256d47f
 
 
 
 
 
 
64b1440
256d47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b1440
256d47f
64b1440
256d47f
64b1440
256d47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b1440
 
256d47f
 
64b1440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256d47f
 
 
 
 
 
 
 
 
 
 
64b1440
256d47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b1440
256d47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b1440
256d47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b1440
256d47f
 
 
 
 
 
 
 
 
 
 
 
 
64b1440
256d47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b1440
 
256d47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b1440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470

import json
import base64
import os
import re

import streamlit as st
from langchain.llms import OpenAI
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from PyPDF2 import PdfReader
import streamlit as st
from streamlit_option_menu import option_menu
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import OpenAIEmbeddings
import pandas as pd
from textwrap import dedent
from streamlit_oauth import OAuth2Component


openai_api_key = os.environ.get('OPENAI_API_KEY')
CLIENT_ID = os.environ.get("CLIENT_ID")
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
REDIRECT_URI = os.environ.get("REDIRECT_URI")


# Initialize session states
if "generated" not in st.session_state:
    st.session_state["generated"] = []
if "past" not in st.session_state:
    st.session_state["past"] = []
if "input" not in st.session_state:
    st.session_state["input"] = ""
if "vectorstore" not in st.session_state:
    st.session_state["vectorstore"] = None
if "rfp_details" not in st.session_state:
    st.session_state["rfp_details"] = ""
if "is_data_processed" not in st.session_state:
    st.session_state["is_data_processed"] = False
if "user_stories" not in st.session_state:
    st.session_state["user_stories"] = ""
if "user_stories_json" not in st.session_state:
    st.session_state["user_stories_json"] = []
if "is_user_stories_created" not in st.session_state:
    st.session_state["is_user_stories_created"] = False
if "rfp_summary" not in st.session_state:
    st.session_state["rfp_summary"] = ""


bot_template = """As a highly intelligent and powerful chatbot, your personality is a business analyst's responsibility.
Your designated name is RFP Bot. Initiate the conversation with a warm greeting only when the user asks something for the first time. Subsequent interactions can skip the greeting.
Your generated responses should be comprehensive, utilizing pointers when necessary for an enhanced user experience. Provide detailed answers, and if the user's input isn't related to personality, respond politely with an apology, encouraging them to ask questions related to your established personality. Keep your responses concise and accurate, seeking additional information from the user when required.
Incorporate the provided context and any relevant information from the chat history into your responses. If the user's input is related to the context or question, articulate your answer accordingly.
{context}
Human: {input}
Assistant:"""

# user_story_template = """Given the project details with the following parameters:

# **Project Name:** {project_name}

# **RFP details:**
# {rfp_details}

# As a business analyst, analyze the given RFP details to delineate epics for the project. Break down the epics into features and features into individual requirements, adhering to the agile software development INVEST principles. Subsequently, generate user stories for each requirement. Ensure each story follows the "As a, I want to, So that" format and strictly avoid combining stories together.

# **Examples**

# 1. Epic: User Authentication and Account Security Enhancements

#   1. Feature 1: User Registration
#     - Story 1:
#      - As a user, I want to provide my basic information, such as name and email address during sign-up, so that the platform can create a personalized account for me.
#     - Story 2:
#      - As a user, I want the sign-up form to include a CAPTCHA verification step, so that the system can prevent automated bots from creating fake accounts.
#     - Story 3:
#      - As a user, I want the sign-up form to include optional fields for additional profile information, so that I can customize my account based on my interests.
#     - Story 4:
#      - As a user, I want the platform to clearly communicate its data usage and privacy policy during the sign-up process, so that I can make an informed decision before creating an account.
     
#   2. Feature 2: User Authentication
#     - Story 1:
#      - As a registered user, I want to log in securely with my credentials, so that I can access personalized account information.
#     - Story 2:
#      - As a registered user, I want the login page to feature a "Forgot Password" link, so that I can easily initiate the password recovery process if needed.
#     - Story 3:
#      - As a user concerned about account security, I want the platform to send a notification email whenever my account is accessed from a new device, so that I can be aware of any potential unauthorized access.
#     - Story 4:
#      - As a returning user, I want the option to enable two-factor authentication (2FA) for an extra layer of security during the login process, so that my account remains protected.
#     - Story 5:
#      - As a user striving for a seamless experience, I want the platform to offer a "Stay Logged In" option during login, so that I don't have to enter my credentials every time I revisit the site.
#     - Story 6:
#      - As a user with multiple devices, I want the ability to log out remotely from any active sessions through the account settings, so that I can maintain control over my account access.
     
#   3. Feature 3: Account Security Enhancements
#     - Story 1:
#       - As a security-conscious user, I want the platform to regularly prompt me to update my password and provide guidance on creating a secure password to enhance the overall security of my account.
#     - Story 2:
#       - As a user who values privacy, I want the platform to automatically log me out after a period of inactivity, ensuring that my account is secure even if I forget to log out manually.
#     - Story 3:
#       - As a user concerned about account recovery, I want the platform to offer multiple secure account recovery options, such as email verification and phone number verification, to ensure I can regain access to my account if needed.
# """

user_story_template = """Given the project details with the following parameters:

**Project Name:** {project_name}

**RFP details:**
{rfp_details}

As a business analyst, analyze the given RFP details to delineate epics for the project. Break down the epics into features and features into individual requirements, adhering to the agile software development INVEST principles (where I refers to Independent (not dependent on other work deliverables), N refers to Negotiable (allows for best practices),  V refers to valuable (provides working functionality), E refers to Estimable (allows clear work estimates), S refers to Small (sized for the team's sprint), T refers to Testable (can be measured to ensure it meets customer expectations)). Subsequently, generate user stories for each requirement. Ensure each story follows the "As a, I want to, So that" format and strictly avoid combining stories together.

**Examples**

1. Epic: User Authentication and Account Security Enhancements

  1. Feature 1: User Registration
    - Story 1:
     - As a user, I want to provide my basic information, such as name and email address during sign-up, so that the platform can create a personalized account for me.
    - Story 2:
     - As a user, I want the sign-up form to include a CAPTCHA verification step, so that the system can prevent automated bots from creating fake accounts.
    - Story 3:
     - As a user, I want the sign-up form to include optional fields for additional profile information, so that I can customize my account based on my interests.
    - Story 4:
     - As a user, I want the platform to clearly communicate its data usage and privacy policy during the sign-up process, so that I can make an informed decision before creating an account.
     
  2. Feature 2: User Authentication
    - Story 1:
     - As a registered user, I want to log in securely with my credentials, so that I can access personalized account information.
    - Story 2:
     - As a registered user, I want the login page to feature a "Forgot Password" link, so that I can easily initiate the password recovery process if needed.
    - Story 3:
     - As a user concerned about account security, I want the platform to send a notification email whenever my account is accessed from a new device, so that I can be aware of any potential unauthorized access.
    - Story 4:
     - As a returning user, I want the option to enable two-factor authentication (2FA) for an extra layer of security during the login process, so that my account remains protected.
    - Story 5:
     - As a user striving for a seamless experience, I want the platform to offer a "Stay Logged In" option during login, so that I don't have to enter my credentials every time I revisit the site.
    - Story 6:
     - As a user with multiple devices, I want the ability to log out remotely from any active sessions through the account settings, so that I can maintain control over my account access.
     
  3. Feature 3: Account Security Enhancements
    - Story 1:
      - As a security-conscious user, I want the platform to prompt me every 15 days to update my password and provide guidance on creating a secure password to enhance the overall security of my account.
    - Story 2:
      - As a user who values privacy, I want the platform to automatically log me out after 5 minutes of inactivity, ensuring that my account is secure even if I forget to log out manually.
    - Story 3:
      - As a user concerned about account recovery, I want the platform to offer  email verification and phone number verification  account recovery options, to ensure I can regain access to my account if needed.
"""

summary_template = """Given the project details with the following parameters:

**Project Name:** {project_name}

**RFP details:**
{rfp_details}

As a business analyst, analyze the given RFP details and give the detailed summary of the project.

"""

convert_json_template = """
Given the project user stories:

**user_stories:** {user_stories}

Create a JSON representation that captures the structure and details of each epic and feature, including their respective stories. Ensure that the JSON format is well-organized, with clear hierarchies for epic,features and stories.

Example structure:

{json_structure}
"""

json_structure = {
    "epics": [
        {
            "name": "App Performance and Efficiency",
            "features": [
                {
                    "name": "Fast and Zero Load Time",
                    "stories": [
                        {
                            "name": "Story 1",
                            "description": "As a user, I want the app to load quickly with zero load time, so that I can access news content efficiently."
                        },
                        {
                            "name": "Story 2",
                            "description": "As a user, I want the installation process of the mobile app to be quick and hassle-free, so that I can start using the app immediately after download without any complications."
                        }
                    ]
                }
            ]
        }
    ]
}


#function to create a new rfp and clear the existing rfp data

def clear_rfp_data():
    st.session_state.clear()
    
#function to logout user from app    
    
def log_out_user():
    st.session_state.clear()
    st.rerun()
    
#function to validate the email     
    
def validate_email(email):
    # Define the pattern for the company email
    pattern = r'^[a-zA-Z]+@coffeebeans\.io$'

    # Use the re.match function to check if the email matches the pattern
    match = re.match(pattern, email)

    # Return True if there is a match, indicating a valid email
    return match is not None


# rfp summary creater

summary_prompt = PromptTemplate(
    input_variables=["project_name", "rfp_details"],
    template=summary_template
)

summary_chain = LLMChain(
    llm=OpenAI(model_name='gpt-3.5-turbo-16k', temperature=0.7,
               api_key= openai_api_key),
    prompt=summary_prompt,
    verbose="true",
)

# process rfp data from pdf file

def process_rfp_data(project_name, file):
    if project_name and file:
        loader = PdfReader(file)
        for i, page in enumerate(loader.pages):
            content = page.extract_text()
            if content:
                temp = st.session_state["rfp_details"]
                st.session_state["rfp_details"] = temp+content
        text_splitter = CharacterTextSplitter(
            separator='\n', chunk_size=1000, chunk_overlap=150, length_function=len)
        texts = text_splitter.split_text(
            st.session_state["rfp_details"])
        st.session_state["vectorstore"] = Chroma().from_texts(texts, embedding=OpenAIEmbeddings(
            openai_api_key=openai_api_key))
        st.session_state.project_name = project_name
        st.session_state["rfp_summary"] = summary_chain.run(
            {"project_name": st.session_state["project_name"], "rfp_details": st.session_state["rfp_details"]})
        st.session_state["is_data_processed"] = True
        st.success('data processed sucessfully')


# genrate bot conversastion

bot_prompt = PromptTemplate(
    input_variables=["context", "input"],
    template=bot_template
)

bot_chain = LLMChain(
    llm=OpenAI(model_name='gpt-3.5-turbo-16k', temperature=0.7,
               api_key= openai_api_key),
    prompt=bot_prompt,
    verbose="true",
)


def genrate_bot_result():
    if len(st.session_state["input"]) > 0:
        db = st.session_state["vectorstore"]
        context = db.similarity_search(st.session_state["input"])
        inputs = {
            "context": context[0].page_content,
            "input": st.session_state["input"]
        }
        output = bot_chain.run(inputs)
        st.session_state.past.append(st.session_state["input"])
        st.session_state.generated.append(output)
        st.session_state["input"] = ""


# genrate user stories user_story_template = """

user_story_prompt = PromptTemplate(
    input_variables=["project_name", "rfp_details"],
    template=user_story_template
)

user_story_chain = LLMChain(
    llm=OpenAI(model_name='gpt-3.5-turbo-16k', temperature=0.7,
               api_key= openai_api_key),
    prompt=user_story_prompt,
    verbose="true",
)

# prompt template for json

json_prompt_template = PromptTemplate(
    input_variables=["user_stories", "json_structure"],
    template=convert_json_template
)

json_chain = LLMChain(
    llm=OpenAI(model_name='gpt-3.5-turbo-16k', temperature=0.7,
               api_key= openai_api_key),
    prompt=json_prompt_template,
    verbose="true",
)


def genrate_user_stories():
    output = user_story_chain.run(
        {"project_name": st.session_state["project_name"], "rfp_details": st.session_state["rfp_details"]})
    st.session_state["user_stories"] = output

    json_response = json_chain.run(
        {"user_stories": st.session_state["user_stories"], "json_structure": json_structure})

    user_stories_data = json.loads(json_response)

    for epic_data in user_stories_data['epics']:
      epic = epic_data["name"]
      for feature_data in epic_data['features']:
       feature = feature_data["name"]
       for story in feature_data["stories"]:
          st.session_state.user_stories_json.append(
                {"epic": epic, "Feature": feature, "Story Description": story["description"]})
    st.session_state["is_user_stories_created"] = True


def genrate_technical_stories():
    print('genrate technical stories calling')


def export_stories():
    data_df = pd.DataFrame(st.session_state["user_stories_json"])
    csv_data = data_df.to_csv(index=False)
    b64 = base64.b64encode(csv_data.encode()).decode()
    href = f'<a href="data:file/csv;base64,{b64}" download="data.csv">Download CSV File</a>'
    st.markdown(href, unsafe_allow_html=True)
    print('generate technical stories calling')


def main():
    if "input" not in st.session_state:
        st.session_state["input"] = ""

    with st.sidebar:
        menu_choice = option_menu(menu_title="RFPStoryCraft", options=[
                                  'Home', 'RFP Bot', 'User Stories', "Summary"], icons=['house','list-task', 'book', 'book'])
        if st.session_state["is_data_processed"] == True:
            st.button('Clear RFP Data', on_click=clear_rfp_data)
        if "auth"  in st.session_state:
         st.button('Log out', on_click=log_out_user)

    if menu_choice == 'Home':
        with st.form("my_form"):
            project_name = st.text_input('Project Name', key='Project Name',
                                         type="default", placeholder='Project Name')
            file = st.file_uploader('Document', type='pdf')

            submitted = st.form_submit_button("Process Data")

            if submitted:
                if project_name and file:
                    process_rfp_data(project_name, file)
                else:
                    st.warning(
                        'project_name and file are required to create create stories', icon="⚠️")

    if menu_choice == 'RFP Bot':
        if st.session_state["is_data_processed"] == True:
            st.title(" RFP Chatbot ")
            st.subheader(" Powered by Coffeebeans")
            st.text_input("You: ", st.session_state["input"], key="input",
                          placeholder="Your AI assistant here! Ask me Queries related to RFP",
                          on_change=genrate_bot_result(),
                          label_visibility='hidden')
            with st.container():
                for i in range(len(st.session_state['generated'])-1, -1, -1):
                    st.success(st.session_state["generated"][i], icon="🤖")
                    st.info(st.session_state["past"][i], icon="🧐")
        else:
            st.warning(
                'Plesase Process RFP Details to access this feature', icon="⚠️")

    if menu_choice == 'User Stories':
        if st.session_state["is_data_processed"] == True:
            st.title("User Stories")
            st.button("Genrate User Stories", type="primary",
                      on_click=genrate_user_stories)
            if st.session_state["is_user_stories_created"] == True:
                st.button("Genrate technical Stories", type="primary",
                          on_click=genrate_technical_stories)
                st.button("Export Stories", on_click=export_stories)
            with st.container():
                df = pd.DataFrame(st.session_state["user_stories_json"])
                st.table(df)
        else:
            st.warning(
                'Plesase Process RFP Details to access this feature', icon="⚠️")

    if menu_choice == 'Summary':
        if st.session_state["is_data_processed"] == True:
            st.title("Summary")
            with st.container():
                st.markdown(st.session_state["rfp_summary"])
        else:
            st.warning(
                'Plesase Process RFP Details to access this feature', icon="⚠️")


if __name__ == "__main__":
    if "auth" not in st.session_state:
        st.title("SIGN UP TO ACCESS THE APP!")
        AUTHORIZE_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth"
        TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token"
        REVOKE_ENDPOINT = "https://oauth2.googleapis.com/revoke"

        # create a button to start the OAuth2 flow
        oauth2 = OAuth2Component(
            CLIENT_ID,
            CLIENT_SECRET,
            AUTHORIZE_ENDPOINT,
            TOKEN_ENDPOINT,
            TOKEN_ENDPOINT,
            REVOKE_ENDPOINT,
        )
        result = oauth2.authorize_button(
            name="Continue with Google",
            icon="https://www.google.com.tw/favicon.ico",
            redirect_uri="https://huggingface.co/spaces/coffeebeans-ai/rfp_to_story",
            scope="openid email profile",
            key="google",
            height=600,
            width=500,
            extras_params={"prompt": "consent", "access_type": "offline"},
            use_container_width=False,
            pkce="S256",
        )

        if result:
            # decode the id_token jwt and get the user's email address
            id_token = result["token"]["id_token"]
            # verify the signature is an optional step for security
            payload = id_token.split(".")[1]
            # add padding to the payload if needed
            payload += "=" * (-len(payload) % 4)
            payload = json.loads(base64.b64decode(payload))
            email = payload["email"]
            st.session_state["auth"] = email
            st.session_state["token"] = result["token"]
            is_valid = validate_email(st.session_state["auth"])
            
            if is_valid:
             st.rerun()
            else:
             st.warning("Unauthorized. you are not authorized to access this app.")
        
    else:
        main()