Spaces:
Runtime error
Runtime error
Update pages/chatbot.py
Browse files- pages/chatbot.py +57 -70
pages/chatbot.py
CHANGED
|
@@ -1,86 +1,73 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
"
|
| 11 |
-
"addiction_level": ["addicted", "addiction level"],
|
| 12 |
-
"reduce_addiction": ["reduce addiction", "how to stop"]
|
| 13 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
if p in user_input.lower():
|
| 25 |
-
platform = p.capitalize()
|
| 26 |
-
break
|
| 27 |
-
|
| 28 |
-
return intent, platform
|
| 29 |
-
|
| 30 |
-
def generate_response(intent, platform, data):
|
| 31 |
-
if intent == "time_spent":
|
| 32 |
-
if platform:
|
| 33 |
-
filtered_data = data[data["Platform"] == platform]
|
| 34 |
-
if not filtered_data.empty:
|
| 35 |
-
avg_time_spent = filtered_data["Total Time Spent"].mean()
|
| 36 |
-
return f"The average time spent on {platform} is {avg_time_spent:.2f} minutes."
|
| 37 |
-
else:
|
| 38 |
-
return f"No data available for {platform}."
|
| 39 |
-
else:
|
| 40 |
-
return "Please specify a platform (e.g., TikTok, Instagram)."
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return f"The average addiction level for {platform} users is {avg_addiction:.2f}."
|
| 48 |
-
else:
|
| 49 |
-
return f"No data available for {platform}."
|
| 50 |
-
else:
|
| 51 |
-
return "Please specify a platform (e.g., TikTok, Instagram)."
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"1. Set daily screen time limits.\n"
|
| 57 |
-
"2. Take regular breaks from social media.\n"
|
| 58 |
-
"3. Identify triggers (e.g., boredom, procrastination) and find alternative activities.\n"
|
| 59 |
-
"4. Use apps to monitor and limit your usage."
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
st.title("Social Media Addiction Chatbot")
|
| 68 |
-
st.write("Ask questions about your social media usage or addiction!")
|
| 69 |
-
|
| 70 |
-
# Chat interface
|
| 71 |
-
user_input = st.chat_input("Ask a question...")
|
| 72 |
if user_input:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# Parse user input
|
| 76 |
-
intent, platform = parse_user_input(user_input)
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
-
|
| 84 |
# import streamlit as st
|
| 85 |
# import pandas as pd
|
| 86 |
# import google.generativeai as genai
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
+
# Set up Gemini API configuration
|
| 5 |
+
GEMINI_API_KEY = "AIzaSyDnQfeYA4fQ-gFUyY16611nZv7rvdQ9Ii0"
|
| 6 |
+
GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent"
|
| 7 |
|
| 8 |
+
# Function to call Gemini API
|
| 9 |
+
def query_gemini(prompt):
|
| 10 |
+
headers = {
|
| 11 |
+
"Content-Type": "application/json"
|
|
|
|
|
|
|
| 12 |
}
|
| 13 |
+
data = {
|
| 14 |
+
"contents": [{
|
| 15 |
+
"parts": [{"text": prompt}]
|
| 16 |
+
}]
|
| 17 |
+
}
|
| 18 |
+
params = {
|
| 19 |
+
"key": GEMINI_API_KEY
|
| 20 |
+
}
|
| 21 |
+
response = requests.post(GEMINI_API_URL, headers=headers, json=data, params=params)
|
| 22 |
|
| 23 |
+
if response.status_code == 200:
|
| 24 |
+
# Extract the response text from the JSON
|
| 25 |
+
response_data = response.json()
|
| 26 |
+
try:
|
| 27 |
+
return response_data['candidates'][0]['content']['parts'][0]['text']
|
| 28 |
+
except KeyError:
|
| 29 |
+
return "Sorry, I couldn't process that request."
|
| 30 |
+
else:
|
| 31 |
+
return f"Error: {response.status_code}, {response.text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Streamlit App
|
| 34 |
+
def main():
|
| 35 |
+
st.set_page_config(page_title="Gemini Chatbot", page_icon="🤖")
|
| 36 |
+
st.title("🤖 Gemini-Powered Chatbot")
|
| 37 |
+
st.markdown("Ask me anything! I'll do my best to help.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Initialize session state for chat history
|
| 40 |
+
if "chat_history" not in st.session_state:
|
| 41 |
+
st.session_state.chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Display chat history
|
| 44 |
+
for message in st.session_state.chat_history:
|
| 45 |
+
with st.chat_message(message["role"]):
|
| 46 |
+
st.markdown(message["content"])
|
| 47 |
|
| 48 |
+
# User input
|
| 49 |
+
user_input = st.chat_input("Type your question here...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
if user_input:
|
| 51 |
+
# Add user message to chat history
|
| 52 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Display user message
|
| 55 |
+
with st.chat_message("user"):
|
| 56 |
+
st.markdown(user_input)
|
| 57 |
+
|
| 58 |
+
# Query Gemini API
|
| 59 |
+
with st.spinner("Thinking..."):
|
| 60 |
+
gemini_response = query_gemini(user_input)
|
| 61 |
+
|
| 62 |
+
# Add assistant message to chat history
|
| 63 |
+
st.session_state.chat_history.append({"role": "assistant", "content": gemini_response})
|
| 64 |
+
|
| 65 |
+
# Display assistant message
|
| 66 |
+
with st.chat_message("assistant"):
|
| 67 |
+
st.markdown(gemini_response)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
+
main()
|
| 71 |
# import streamlit as st
|
| 72 |
# import pandas as pd
|
| 73 |
# import google.generativeai as genai
|