Upload 3 files
Browse files- README.md +2 -12
- app.py +94 -0
- requirements.txt +11 -0
README.md
CHANGED
@@ -1,12 +1,2 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
emoji: 🐨
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: green
|
6 |
-
sdk: streamlit
|
7 |
-
sdk_version: 1.44.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
# gemini-pro-streamlit-chatbot
|
2 |
+
This repository is about building a chatbot using Google's Gemini-Pro with streamlit.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import google.generativeai as gen_ai
|
5 |
+
|
6 |
+
# Load environment variables
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Configure Streamlit page settings
|
10 |
+
st.set_page_config(
|
11 |
+
page_title="Smart Waste Management System",
|
12 |
+
page_icon=":recycle:", # Favicon emoji
|
13 |
+
layout="centered", # Page layout option
|
14 |
+
)
|
15 |
+
|
16 |
+
# Retrieve the Google API key from the environment
|
17 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
18 |
+
|
19 |
+
# Check if the API key is loaded
|
20 |
+
if not GOOGLE_API_KEY:
|
21 |
+
st.error("API key not found! Please set the GOOGLE_API_KEY in your .env file.")
|
22 |
+
st.stop()
|
23 |
+
|
24 |
+
# Configure the Generative AI model
|
25 |
+
try:
|
26 |
+
gen_ai.configure(api_key=GOOGLE_API_KEY)
|
27 |
+
model = gen_ai.GenerativeModel("gemini-pro")
|
28 |
+
except Exception as e:
|
29 |
+
st.error(f"Error initializing the Gemini-Pro model: {e}")
|
30 |
+
st.stop()
|
31 |
+
|
32 |
+
# Function to translate roles between Gemini-Pro and Streamlit terminology
|
33 |
+
def translate_role_for_streamlit(user_role):
|
34 |
+
return "assistant" if user_role == "model" else user_role
|
35 |
+
|
36 |
+
# Initialize the chat session if not already present in session state
|
37 |
+
if "chat_session" not in st.session_state:
|
38 |
+
try:
|
39 |
+
st.session_state.chat_session = model.start_chat(history=[])
|
40 |
+
except Exception as e:
|
41 |
+
st.error(f"Error initializing chat session: {e}")
|
42 |
+
st.stop()
|
43 |
+
|
44 |
+
# Display the app's title
|
45 |
+
st.title("♻️ Smart Waste Management System")
|
46 |
+
|
47 |
+
# Introduction and instructions
|
48 |
+
st.markdown(
|
49 |
+
"""
|
50 |
+
Welcome to the Smart Waste Management System! This tool helps citizens, municipal workers,
|
51 |
+
recycling companies, and biogas plants collaborate efficiently for effective waste management.
|
52 |
+
|
53 |
+
**Key Features:**
|
54 |
+
- **Citizen Role:** Report waste collection issues and track garbage pickup.
|
55 |
+
- **Municipal Workers:** Manage schedules and coordinate garbage segregation.
|
56 |
+
- **Recycling Companies:** View and respond to requests for plastic waste.
|
57 |
+
- **Biogas Plants:** Manage bio-waste for energy production.
|
58 |
+
"""
|
59 |
+
)
|
60 |
+
|
61 |
+
# Input fields for user role and message
|
62 |
+
user_role = st.selectbox("Select Your Role:", ["Citizen", "Municipal Worker", "Recycling Company", "Biogas Plant"])
|
63 |
+
|
64 |
+
user_prompt = st.chat_input(f"[{user_role}] Enter your query or task...")
|
65 |
+
if user_prompt:
|
66 |
+
# Add the user's message to the chat and display it
|
67 |
+
st.chat_message("user").markdown(f"**{user_role}:** {user_prompt}")
|
68 |
+
|
69 |
+
# Customize the prompt based on the user role
|
70 |
+
role_specific_prompt = (
|
71 |
+
f"You are assisting a {user_role} in a waste management system. The user says: {user_prompt}"
|
72 |
+
)
|
73 |
+
|
74 |
+
# Send the prompt to Gemini-Pro and get the response
|
75 |
+
try:
|
76 |
+
gemini_response = st.session_state.chat_session.send_message(role_specific_prompt)
|
77 |
+
# Display Gemini-Pro's response
|
78 |
+
with st.chat_message("assistant"):
|
79 |
+
st.markdown(gemini_response.text)
|
80 |
+
except Exception as e:
|
81 |
+
st.error(f"Error processing your message: {e}")
|
82 |
+
|
83 |
+
# Add a sidebar for additional information
|
84 |
+
st.sidebar.title("About")
|
85 |
+
st.sidebar.markdown(
|
86 |
+
"""
|
87 |
+
The **Smart Waste Management System** aims to:
|
88 |
+
- Promote efficient segregation and recycling of waste.
|
89 |
+
- Facilitate collaboration between stakeholders.
|
90 |
+
- Reduce environmental impact and enhance sustainability.
|
91 |
+
|
92 |
+
**Need Help?** Use the chat to ask questions or provide feedback.
|
93 |
+
"""
|
94 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
google-generativeai==0.3.2
|
3 |
+
deep-translator
|
4 |
+
Pillow
|
5 |
+
graphviz
|
6 |
+
streamlit==1.27.2
|
7 |
+
openai>=1.0.0
|
8 |
+
python-dotenv==1.0.0
|
9 |
+
Pillow==10.0.0
|
10 |
+
graphviz==0.20.1
|
11 |
+
|