Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import requests | |
| import json | |
| # Set up Gemini API key | |
| GEMINI_API_KEY = "AIzaSyCbkm1DvOWXeQxrH2BYz3PrRbpdAseor20" | |
| API_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_API_KEY}" | |
| # Define addiction assessment questions | |
| questions = [ | |
| "How many hours do you spend on social media daily? (0-24)", | |
| "How often do you check social media? (Rarely, Sometimes, Often, Always)", | |
| "Does social media affect your productivity? (Yes/No)", | |
| "Do you feel anxious when not using social media? (Yes/No)", | |
| "Have you tried to reduce your usage but failed? (Yes/No)" | |
| ] | |
| # Mapping responses to addiction scores | |
| def calculate_addiction_score(responses): | |
| score = 0 | |
| # Question 1: Hours spent on social media (0-10) | |
| score += int(responses[0]) if responses[0].isdigit() else 0 | |
| # Question 2: Frequency of checking | |
| frequency_scores = {"Rarely": 2, "Sometimes": 4, "Often": 7, "Always": 10} | |
| score += frequency_scores.get(responses[1], 0) | |
| # Yes/No questions (Questions 3-5) | |
| for i in range(2, 5): | |
| if responses[i].strip().lower() == "yes": | |
| score += 2 # Add 2 points for each 'Yes' | |
| # Normalize score (Max possible is 30) | |
| return min(score, 10) # Keep score between 0-10 | |
| def get_gemini_response(score): | |
| """Send addiction level to Gemini API and get a response.""" | |
| headers = {"Content-Type": "application/json"} | |
| data = { | |
| "contents": [{"parts": [{"text": f"The user has a social media addiction score of {score}/10. Provide advice accordingly."}]}] | |
| } | |
| response = requests.post(API_URL, headers=headers, json=data) | |
| if response.status_code == 200: | |
| return response.json()["candidates"][0]["content"]["parts"][0]["text"] | |
| else: | |
| return "โ ๏ธ Error: Unable to get response. Check API key and usage limits." | |
| # Streamlit UI | |
| st.title("๐ฑ Social Media Addiction Test") | |
| # Initialize session state | |
| if "responses" not in st.session_state: | |
| st.session_state.responses = [] | |
| if "question_index" not in st.session_state: | |
| st.session_state.question_index = 0 | |
| if "addiction_score" not in st.session_state: | |
| st.session_state.addiction_score = None | |
| # Ask Questions One by One | |
| if st.session_state.question_index < len(questions): | |
| user_response = st.text_input(questions[st.session_state.question_index]) | |
| if user_response: | |
| st.session_state.responses.append(user_response) | |
| st.session_state.question_index += 1 | |
| st.rerun() | |
| # Once all questions are answered, calculate addiction level | |
| elif st.session_state.addiction_score is None: | |
| st.session_state.addiction_score = calculate_addiction_score(st.session_state.responses) | |
| addiction_level = st.session_state.addiction_score | |
| st.subheader(f"๐ Your Social Media Addiction Score: {addiction_level}/10") | |
| # Get advice from Gemini API | |
| advice = get_gemini_response(addiction_level) | |
| st.write(advice) | |
| # Reset session for next test | |
| if st.button("Retake Test"): | |
| st.session_state.responses = [] | |
| st.session_state.question_index = 0 | |
| st.session_state.addiction_score = None | |
| st.rerun() | |
| # import streamlit as st | |
| # import requests | |
| # import json | |
| # # Set up Gemini API key | |
| # GEMINI_API_KEY = "AIzaSyCbkm1DvOWXeQxrH2BYz3PrRbpdAseor20" | |
| # API_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_API_KEY}" | |
| # def get_gemini_response(user_input): | |
| # """Send request to Gemini API and return the response.""" | |
| # headers = {"Content-Type": "application/json"} | |
| # data = { | |
| # "contents": [{"parts": [{"text": user_input}]}] | |
| # } | |
| # response = requests.post(API_URL, headers=headers, json=data) | |
| # if response.status_code == 200: | |
| # return response.json()["candidates"][0]["content"]["parts"][0]["text"] | |
| # else: | |
| # return "โ ๏ธ Error: Unable to get response. Check API key and usage limits." | |
| # # Streamlit UI | |
| # st.title("๐ค Gemini AI Chatbot") | |
| # st.sidebar.header("Chatbot Settings") | |
| # st.sidebar.write("This bot gives **concise & to-the-point** responses.") | |
| # # Initialize chat history | |
| # if "messages" not in st.session_state: | |
| # st.session_state.messages = [] | |
| # # Display chat history | |
| # for msg in st.session_state.messages: | |
| # with st.chat_message(msg["role"]): | |
| # st.write(msg["content"]) | |
| # # User input | |
| # user_input = st.chat_input("Ask me anything...") | |
| # if user_input: | |
| # st.session_state.messages.append({"role": "user", "content": user_input}) | |
| # # Get response from Gemini | |
| # bot_reply = get_gemini_response(user_input) | |
| # st.session_state.messages.append({"role": "assistant", "content": bot_reply}) | |
| # # Display bot response | |
| # with st.chat_message("assistant"): | |
| # st.write(bot_reply) | |