File size: 5,129 Bytes
d94942e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dotenv import load_dotenv
import os
from PyPDF2 import PdfReader
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import OpenAIEmbeddings
import streamlit as st
from textwrap import dedent
from Prompts_and_Chains import LLMChains
from Templates import json_structure
import json
from Utils import estimate_to_value


class RFPProcessor:
    def __init__(self):
        load_dotenv()
        self.openai_api_key = os.getenv("OPENAI_API_KEY")
        self.chains_obj = LLMChains()

    def generate_estimations(self, tech_leads, senior_developers, junior_developers):
        print(st.session_state["user_stories_json"])
        inputs = {
            "project_summary": st.session_state["rfp_summary"],
            "user_stories": st.session_state["user_stories_json"],
            "tech_leads": tech_leads,
            "senior_developers": senior_developers,
            "junior_developers": junior_developers,
        }

        data = self.chains_obj.estimations_chain.run(inputs)
        estimation_json_data = json.loads(data)

        for epic_data in estimation_json_data["epics"]:
            epic = epic_data["name"]
            for feature_data in epic_data["features"]:
                feature = feature_data["name"]
                for story in feature_data["stories"]:
                    average = estimate_to_value(story["estimate"])
                    st.session_state.estimation_data.append(
                        {
                            "epic": epic,
                            "Feature": feature,
                            "Story Description": story["description"],
                            "Story Estimation": story["estimate"],
                            "Story Effort": story["effort"],
                            "Average Estimate": average,
                            "Story Rationale": story["rationale"],
                        }
                    )
        st.session_state["is_estimation_data_created"] = True

    def process_rfp_data(self, 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=self.openai_api_key)
            )

            st.session_state.project_name = project_name
            st.session_state["rfp_summary"] = self.chains_obj.summary_chain.run(
                {
                    "project_name": st.session_state["project_name"],
                    "rfp_details": dedent(st.session_state["rfp_details"]),
                }
            )

            st.session_state["is_data_processed"] = True
            st.success("data processed sucessfully")

    def genrate_bot_result(self):
        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 = self.chains_obj.bot_chain.run(inputs)
            st.session_state.past.append(st.session_state["input"])
            st.session_state.generated.append(output)
            st.session_state["input"] = ""

    def genrate_user_stories(self):
        output = self.chains_obj.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 = self.chains_obj.json_chain.run(
            {
                "user_stories": st.session_state["user_stories"],
                "json_structure": json_structure,
            }
        )
        user_stories_data = json.loads(json_response)
        print(user_stories_data)
        st.session_state["user_stories_json"] = user_stories_data

        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_data.append(
                        {
                            "epic": epic,
                            "Feature": feature,
                            "Story Description": story["description"],
                        }
                    )
        st.session_state["is_user_stories_created"] = True