Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import google.generativeai as geneai
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
# Load environment variables from .env file
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Apply custom CSS for styling
|
10 |
+
def add_custom_css():
|
11 |
+
st.markdown("""
|
12 |
+
<style>
|
13 |
+
/* Background color */
|
14 |
+
.stApp {
|
15 |
+
background-color: #f0f2f6;
|
16 |
+
background-image: linear-gradient(315deg, #f0f2f6 0%, #c6e5ff 74%);
|
17 |
+
padding: 20px;
|
18 |
+
}
|
19 |
+
/* Sidebar styling */
|
20 |
+
.css-1aumxhk {
|
21 |
+
background-color: #fff;
|
22 |
+
padding: 20px;
|
23 |
+
border-radius: 10px;
|
24 |
+
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
|
25 |
+
}
|
26 |
+
/* Text input styling */
|
27 |
+
.stTextInput, .stTextArea, .stSelectbox {
|
28 |
+
border-radius: 10px;
|
29 |
+
border: 1px solid #dcdcdc;
|
30 |
+
padding: 10px;
|
31 |
+
}
|
32 |
+
/* Buttons */
|
33 |
+
.stButton button {
|
34 |
+
background-color: #007bff;
|
35 |
+
color: white;
|
36 |
+
border: none;
|
37 |
+
border-radius: 10px;
|
38 |
+
padding: 10px 20px;
|
39 |
+
font-size: 16px;
|
40 |
+
}
|
41 |
+
/* Headers */
|
42 |
+
h1, h2, h3 {
|
43 |
+
color: #003366;
|
44 |
+
}
|
45 |
+
/* Success messages */
|
46 |
+
.stAlert {
|
47 |
+
background-color: #e6f7ff;
|
48 |
+
border: 1px solid #91d5ff;
|
49 |
+
border-radius: 10px;
|
50 |
+
}
|
51 |
+
</style>
|
52 |
+
""", unsafe_allow_html=True)
|
53 |
+
|
54 |
+
# Google Vertex AI service class
|
55 |
+
class GoogleVertexAIService:
|
56 |
+
def __init__(self):
|
57 |
+
self.api_key = os.getenv("GOOGLE_API_KEY")
|
58 |
+
if self.api_key:
|
59 |
+
geneai.configure(api_key=self.api_key)
|
60 |
+
self.model_1 = geneai.GenerativeModel("gemini-pro")
|
61 |
+
self.model_2 = geneai.GenerativeModel("gemini-pro")
|
62 |
+
|
63 |
+
def generate_proposal(self, client_data):
|
64 |
+
prompt = self._create_initial_prompt(client_data)
|
65 |
+
try:
|
66 |
+
response = self._generate_text(self.model_1, prompt)
|
67 |
+
refined_prompt = self._create_refined_prompt(client_data, response.text)
|
68 |
+
refined_response = self._generate_text(self.model_2, refined_prompt)
|
69 |
+
return refined_response.text.strip()
|
70 |
+
except Exception as e:
|
71 |
+
print(f"Error generating proposal: {e}")
|
72 |
+
return "An error occurred while generating the proposal."
|
73 |
+
|
74 |
+
def _generate_text(self, model, prompt):
|
75 |
+
try:
|
76 |
+
response = model.generate_content(prompt)
|
77 |
+
return response
|
78 |
+
except Exception as e:
|
79 |
+
print(f"Error in generate_content call: {e}")
|
80 |
+
raise
|
81 |
+
|
82 |
+
def _create_initial_prompt(self, client_data):
|
83 |
+
return (
|
84 |
+
f"Generate a base proposal for {client_data['client_name']} in the {client_data['industry']} industry. "
|
85 |
+
f"The client has specific requirements: {client_data['requirements']}. "
|
86 |
+
"Provide a foundational proposal that can be further refined."
|
87 |
+
)
|
88 |
+
|
89 |
+
def _create_refined_prompt(self, client_data, initial_proposal):
|
90 |
+
return (
|
91 |
+
f"Refine the following proposal for {client_data['client_name']} in the {client_data['industry']} industry. "
|
92 |
+
f"The client has specific requirements: {client_data['requirements']}. "
|
93 |
+
f"Initial proposal: {initial_proposal}. "
|
94 |
+
"Enhance this proposal with more contextually accurate details and ensure it addresses all client needs precisely."
|
95 |
+
)
|
96 |
+
|
97 |
+
# Sidebar input form for client data
|
98 |
+
def proposal_input():
|
99 |
+
st.sidebar.header("π Client Information")
|
100 |
+
client_name = st.sidebar.text_input("Client Name")
|
101 |
+
industry = st.sidebar.selectbox("Industry", ["Tech", "Finance", "Healthcare", "Education", "Other"])
|
102 |
+
requirements = st.sidebar.text_area("Special Requirements")
|
103 |
+
|
104 |
+
client_data = {
|
105 |
+
"client_name": client_name,
|
106 |
+
"industry": industry,
|
107 |
+
"requirements": requirements
|
108 |
+
}
|
109 |
+
|
110 |
+
return client_data
|
111 |
+
|
112 |
+
# Function to generate proposal using Google Generative AI
|
113 |
+
def proposal_generation(client_data):
|
114 |
+
st.header("π Generate Proposal")
|
115 |
+
|
116 |
+
if st.button("Generate Proposal"):
|
117 |
+
with st.spinner("Generating proposal... π€"):
|
118 |
+
google_service = GoogleVertexAIService()
|
119 |
+
proposal = google_service.generate_proposal(client_data)
|
120 |
+
st.session_state['proposal'] = proposal
|
121 |
+
st.success("Proposal generated successfully! π")
|
122 |
+
st.write(proposal)
|
123 |
+
|
124 |
+
# Function to review and edit generated proposal
|
125 |
+
def proposal_review():
|
126 |
+
st.header("βοΈ Review and Edit Proposal")
|
127 |
+
|
128 |
+
if 'proposal' in st.session_state:
|
129 |
+
proposal = st.text_area("Edit Proposal", st.session_state['proposal'], height=400)
|
130 |
+
st.session_state['proposal'] = proposal
|
131 |
+
st.success("Proposal updated successfully! π")
|
132 |
+
else:
|
133 |
+
st.warning("No proposal to review. Please generate a proposal first. π΅οΈββοΈ")
|
134 |
+
|
135 |
+
# Function to handle application settings (placeholder)
|
136 |
+
def settings():
|
137 |
+
st.header("βοΈ Settings")
|
138 |
+
st.write("Here you can configure your AI parameters and other preferences.")
|
139 |
+
# Implement settings form here as needed
|
140 |
+
|
141 |
+
# Main application logic
|
142 |
+
def main():
|
143 |
+
st.title("AI-Powered Proposal Generator π")
|
144 |
+
|
145 |
+
# Add custom CSS
|
146 |
+
add_custom_css()
|
147 |
+
|
148 |
+
# Sidebar navigation
|
149 |
+
menu = ["Create Proposal", "Review Proposal", "Settings"]
|
150 |
+
choice = st.sidebar.selectbox("Menu", menu)
|
151 |
+
|
152 |
+
client_data = proposal_input()
|
153 |
+
st.session_state['client_data'] = client_data
|
154 |
+
|
155 |
+
if choice == "Create Proposal":
|
156 |
+
proposal_generation(client_data)
|
157 |
+
elif choice == "Review Proposal":
|
158 |
+
proposal_review()
|
159 |
+
elif choice == "Settings":
|
160 |
+
settings()
|
161 |
+
|
162 |
+
if __name__ == '__main__':
|
163 |
+
main()
|