File size: 5,823 Bytes
463d67f |
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 |
import streamlit as st
import os
import google.generativeai as geneai
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Apply custom CSS for styling
def add_custom_css():
st.markdown("""
<style>
/* Background color */
.stApp {
background-color: #f0f2f6;
background-image: linear-gradient(315deg, #f0f2f6 0%, #c6e5ff 74%);
padding: 20px;
}
/* Sidebar styling */
.css-1aumxhk {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
}
/* Text input styling */
.stTextInput, .stTextArea, .stSelectbox {
border-radius: 10px;
border: 1px solid #dcdcdc;
padding: 10px;
}
/* Buttons */
.stButton button {
background-color: #007bff;
color: white;
border: none;
border-radius: 10px;
padding: 10px 20px;
font-size: 16px;
}
/* Headers */
h1, h2, h3 {
color: #003366;
}
/* Success messages */
.stAlert {
background-color: #e6f7ff;
border: 1px solid #91d5ff;
border-radius: 10px;
}
</style>
""", unsafe_allow_html=True)
# Google Vertex AI service class
class GoogleVertexAIService:
def __init__(self):
self.api_key = os.getenv("GOOGLE_API_KEY")
if self.api_key:
geneai.configure(api_key=self.api_key)
self.model_1 = geneai.GenerativeModel("gemini-pro")
self.model_2 = geneai.GenerativeModel("gemini-pro")
def generate_proposal(self, client_data):
prompt = self._create_initial_prompt(client_data)
try:
response = self._generate_text(self.model_1, prompt)
refined_prompt = self._create_refined_prompt(client_data, response.text)
refined_response = self._generate_text(self.model_2, refined_prompt)
return refined_response.text.strip()
except Exception as e:
print(f"Error generating proposal: {e}")
return "An error occurred while generating the proposal."
def _generate_text(self, model, prompt):
try:
response = model.generate_content(prompt)
return response
except Exception as e:
print(f"Error in generate_content call: {e}")
raise
def _create_initial_prompt(self, client_data):
return (
f"Generate a base proposal for {client_data['client_name']} in the {client_data['industry']} industry. "
f"The client has specific requirements: {client_data['requirements']}. "
"Provide a foundational proposal that can be further refined."
)
def _create_refined_prompt(self, client_data, initial_proposal):
return (
f"Refine the following proposal for {client_data['client_name']} in the {client_data['industry']} industry. "
f"The client has specific requirements: {client_data['requirements']}. "
f"Initial proposal: {initial_proposal}. "
"Enhance this proposal with more contextually accurate details and ensure it addresses all client needs precisely."
)
# Sidebar input form for client data
def proposal_input():
st.sidebar.header("π Client Information")
client_name = st.sidebar.text_input("Client Name")
industry = st.sidebar.selectbox("Industry", ["Tech", "Finance", "Healthcare", "Education", "Other"])
requirements = st.sidebar.text_area("Special Requirements")
client_data = {
"client_name": client_name,
"industry": industry,
"requirements": requirements
}
return client_data
# Function to generate proposal using Google Generative AI
def proposal_generation(client_data):
st.header("π Generate Proposal")
if st.button("Generate Proposal"):
with st.spinner("Generating proposal... π€"):
google_service = GoogleVertexAIService()
proposal = google_service.generate_proposal(client_data)
st.session_state['proposal'] = proposal
st.success("Proposal generated successfully! π")
st.write(proposal)
# Function to review and edit generated proposal
def proposal_review():
st.header("βοΈ Review and Edit Proposal")
if 'proposal' in st.session_state:
proposal = st.text_area("Edit Proposal", st.session_state['proposal'], height=400)
st.session_state['proposal'] = proposal
st.success("Proposal updated successfully! π")
else:
st.warning("No proposal to review. Please generate a proposal first. π΅οΈββοΈ")
# Function to handle application settings (placeholder)
def settings():
st.header("βοΈ Settings")
st.write("Here you can configure your AI parameters and other preferences.")
# Implement settings form here as needed
# Main application logic
def main():
st.title("AI-Powered Proposal Generator π")
# Add custom CSS
add_custom_css()
# Sidebar navigation
menu = ["Create Proposal", "Review Proposal", "Settings"]
choice = st.sidebar.selectbox("Menu", menu)
client_data = proposal_input()
st.session_state['client_data'] = client_data
if choice == "Create Proposal":
proposal_generation(client_data)
elif choice == "Review Proposal":
proposal_review()
elif choice == "Settings":
settings()
if __name__ == '__main__':
main()
|