Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# YOUR API KEY
|
| 5 |
os.environ["OPENAI_API_KEY"] = "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"
|
| 6 |
|
| 7 |
-
from langchain_openai import ChatOpenAI
|
| 8 |
-
|
| 9 |
-
# Knowledge base
|
| 10 |
KNOWLEDGE = """
|
| 11 |
SR University is located in Warangal, Telangana, India.
|
| 12 |
Computer Science program focuses on AI/ML, DSA, Java/Python, AWS/Azure, software engineering.
|
|
@@ -22,47 +22,84 @@ Internship tips:
|
|
| 22 |
5. Target: Google, Microsoft, Hyderabad/Bangalore startups
|
| 23 |
"""
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
st.title("🧠 RAG Chatbot")
|
| 30 |
-
st.
|
| 31 |
|
| 32 |
-
chatbot = init_chatbot()
|
| 33 |
if "messages" not in st.session_state:
|
| 34 |
st.session_state.messages = []
|
| 35 |
|
| 36 |
-
#
|
| 37 |
for message in st.session_state.messages:
|
| 38 |
with st.chat_message(message["role"]):
|
| 39 |
st.markdown(message["content"])
|
| 40 |
|
| 41 |
# Chat input
|
| 42 |
-
if query := st.chat_input("
|
| 43 |
-
# Add user message
|
| 44 |
st.session_state.messages.append({"role": "user", "content": query})
|
| 45 |
with st.chat_message("user"):
|
| 46 |
st.markdown(query)
|
| 47 |
|
| 48 |
-
# Generate response
|
| 49 |
with st.chat_message("assistant"):
|
| 50 |
with st.spinner('Thinking...'):
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
{KNOWLEDGE}
|
| 54 |
-
|
| 55 |
-
Question: {query}
|
| 56 |
-
|
| 57 |
-
Answer:"""
|
| 58 |
-
|
| 59 |
-
response = chatbot.invoke(prompt).content
|
| 60 |
st.markdown(response)
|
| 61 |
|
| 62 |
-
# Add assistant response to chat history
|
| 63 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 64 |
|
| 65 |
-
# Clear chat
|
| 66 |
-
if st.button("🗑️ Clear Chat"):
|
| 67 |
st.session_state.messages = []
|
| 68 |
st.rerun()
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
+
import time
|
| 4 |
+
from langchain_openai import ChatOpenAI
|
| 5 |
+
from openai import RateLimitError
|
| 6 |
|
| 7 |
+
# YOUR API KEY (with rate limit handling)
|
| 8 |
os.environ["OPENAI_API_KEY"] = "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"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
KNOWLEDGE = """
|
| 11 |
SR University is located in Warangal, Telangana, India.
|
| 12 |
Computer Science program focuses on AI/ML, DSA, Java/Python, AWS/Azure, software engineering.
|
|
|
|
| 22 |
5. Target: Google, Microsoft, Hyderabad/Bangalore startups
|
| 23 |
"""
|
| 24 |
|
| 25 |
+
def get_chat_response(query, max_retries=3):
|
| 26 |
+
"""Handle rate limits with retry + fallback"""
|
| 27 |
+
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
| 28 |
+
|
| 29 |
+
for attempt in range(max_retries):
|
| 30 |
+
try:
|
| 31 |
+
prompt = f"""Use ONLY this context to answer:
|
| 32 |
+
|
| 33 |
+
{KNOWLEDGE}
|
| 34 |
+
|
| 35 |
+
Question: {query}
|
| 36 |
+
|
| 37 |
+
Answer:"""
|
| 38 |
+
|
| 39 |
+
response = llm.invoke(prompt).content
|
| 40 |
+
return response
|
| 41 |
+
|
| 42 |
+
except RateLimitError:
|
| 43 |
+
if attempt < max_retries - 1:
|
| 44 |
+
st.warning(f"Rate limit hit. Retrying in 10s... (Attempt {attempt + 1}/{max_retries})")
|
| 45 |
+
time.sleep(10)
|
| 46 |
+
continue
|
| 47 |
+
else:
|
| 48 |
+
# FALLBACK: Local rule-based response
|
| 49 |
+
return get_fallback_response(query)
|
| 50 |
+
|
| 51 |
+
return "Sorry, service temporarily unavailable. Try again later."
|
| 52 |
+
|
| 53 |
+
def get_fallback_response(query):
|
| 54 |
+
"""FREE fallback - no API needed"""
|
| 55 |
+
query_lower = query.lower()
|
| 56 |
+
|
| 57 |
+
if "university" in query_lower or "sr" in query_lower:
|
| 58 |
+
return "SR University is in Warangal, Telangana. Excellent CS program with AI/ML, DSA, cloud computing focus."
|
| 59 |
+
|
| 60 |
+
elif "internship" in query_lower or "job" in query_lower:
|
| 61 |
+
return """AI/ML Internship Tips:
|
| 62 |
+
1. Solve 300+ LeetCode problems
|
| 63 |
+
2. Build RAG chatbot (this project!)
|
| 64 |
+
3. Apply via AngelList, LinkedIn
|
| 65 |
+
4. Target Hyderabad/Bangalore startups"""
|
| 66 |
+
|
| 67 |
+
elif "project" in query_lower:
|
| 68 |
+
return "Portfolio projects: 1) RAG Chatbot (live now), 2) Object Detection (robotic arm), 3) RL Drone Agent"
|
| 69 |
+
|
| 70 |
+
elif "leetcode" in query_lower:
|
| 71 |
+
return "LeetCode: Easy(100) + Medium(150) + Hard(50) = 300 problems total"
|
| 72 |
+
|
| 73 |
+
else:
|
| 74 |
+
return "Ask about SR University, internships, LeetCode, or AI projects! 💡"
|
| 75 |
|
| 76 |
st.title("🧠 RAG Chatbot")
|
| 77 |
+
st.info("💡 Works even if OpenAI quota exhausted!")
|
| 78 |
|
|
|
|
| 79 |
if "messages" not in st.session_state:
|
| 80 |
st.session_state.messages = []
|
| 81 |
|
| 82 |
+
# Chat history
|
| 83 |
for message in st.session_state.messages:
|
| 84 |
with st.chat_message(message["role"]):
|
| 85 |
st.markdown(message["content"])
|
| 86 |
|
| 87 |
# Chat input
|
| 88 |
+
if query := st.chat_input("Ask about university, internships, projects..."):
|
|
|
|
| 89 |
st.session_state.messages.append({"role": "user", "content": query})
|
| 90 |
with st.chat_message("user"):
|
| 91 |
st.markdown(query)
|
| 92 |
|
|
|
|
| 93 |
with st.chat_message("assistant"):
|
| 94 |
with st.spinner('Thinking...'):
|
| 95 |
+
response = get_chat_response(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
st.markdown(response)
|
| 97 |
|
|
|
|
| 98 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 99 |
|
| 100 |
+
# Clear chat
|
| 101 |
+
if st.sidebar.button("🗑️ Clear Chat"):
|
| 102 |
st.session_state.messages = []
|
| 103 |
st.rerun()
|
| 104 |
+
|
| 105 |
+
st.sidebar.success("✅ Works with/without OpenAI quota!")
|