Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
# YOUR API KEY
|
| 5 |
API_KEY = "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"
|
| 6 |
-
|
| 7 |
os.environ["OPENAI_API_KEY"] = API_KEY
|
| 8 |
|
| 9 |
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
@@ -15,27 +14,25 @@ from langchain_core.output_parsers import StrOutputParser
|
|
| 15 |
|
| 16 |
@st.cache_resource
|
| 17 |
def get_chatbot():
|
| 18 |
-
# Auto-create knowledge
|
| 19 |
if not os.path.exists("knowledge.txt"):
|
| 20 |
with open("knowledge.txt", "w") as f:
|
| 21 |
f.write("""
|
| 22 |
SR University is located in Warangal, Telangana, India.
|
| 23 |
-
|
| 24 |
-
Java/Python programming, Cloud Computing (AWS/Azure), and software engineering.
|
| 25 |
|
| 26 |
-
|
| 27 |
-
Key skills: DSA (LeetCode, GFG), AI projects (robotic arms, drones),
|
| 28 |
cloud certifications, competitive programming.
|
| 29 |
|
| 30 |
-
Internship
|
| 31 |
-
1.
|
| 32 |
-
2.
|
| 33 |
-
3. Apply
|
| 34 |
-
4. Practice system design
|
| 35 |
-
5. Target
|
| 36 |
""")
|
| 37 |
|
| 38 |
-
# Create
|
| 39 |
if not os.path.exists("faiss_index"):
|
| 40 |
loader = TextLoader("knowledge.txt")
|
| 41 |
docs = loader.load()
|
|
@@ -51,11 +48,10 @@ Internship preparation tips:
|
|
| 51 |
vectorstore = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 52 |
retriever = vectorstore.as_retriever()
|
| 53 |
|
| 54 |
-
# LLM
|
| 55 |
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
| 56 |
-
|
| 57 |
prompt = ChatPromptTemplate.from_template(
|
| 58 |
-
"Answer using ONLY this context:\n{context}\n\nQuestion: {question}\
|
| 59 |
)
|
| 60 |
|
| 61 |
def rag_chain(query):
|
|
@@ -63,45 +59,31 @@ Internship preparation tips:
|
|
| 63 |
context = "\n".join([doc.page_content for doc in context_docs])
|
| 64 |
chain = (
|
| 65 |
{"context": lambda x: context, "question": lambda x: query}
|
| 66 |
-
| prompt
|
| 67 |
-
| llm
|
| 68 |
-
| StrOutputParser()
|
| 69 |
)
|
| 70 |
return chain.invoke(query)
|
| 71 |
|
| 72 |
return rag_chain
|
| 73 |
|
| 74 |
-
# Main UI
|
| 75 |
st.title("🧠 RAG Chatbot")
|
| 76 |
-
st.info("💡
|
| 77 |
|
| 78 |
chatbot = get_chatbot()
|
| 79 |
-
|
| 80 |
if "messages" not in st.session_state:
|
| 81 |
st.session_state.messages = []
|
| 82 |
|
| 83 |
-
# Display chat history
|
| 84 |
for message in st.session_state.messages:
|
| 85 |
with st.chat_message(message["role"]):
|
| 86 |
st.markdown(message["content"])
|
| 87 |
|
| 88 |
-
|
| 89 |
-
if prompt := st.chat_input("Ask about university, internships, projects..."):
|
| 90 |
-
# Add user message
|
| 91 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 92 |
with st.chat_message("user"):
|
| 93 |
st.markdown(prompt)
|
| 94 |
|
| 95 |
-
# Generate and display response
|
| 96 |
with st.chat_message("assistant"):
|
| 97 |
-
with st.spinner("
|
| 98 |
response = chatbot(prompt)
|
| 99 |
st.markdown(response)
|
| 100 |
|
| 101 |
-
# Store assistant response
|
| 102 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 103 |
-
|
| 104 |
-
# Sidebar info
|
| 105 |
-
with st.sidebar:
|
| 106 |
-
st.success("✅ RAG Chatbot Live!")
|
| 107 |
-
st.balloons()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
# YOUR API KEY
|
| 5 |
API_KEY = "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"
|
|
|
|
| 6 |
os.environ["OPENAI_API_KEY"] = API_KEY
|
| 7 |
|
| 8 |
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
|
|
| 14 |
|
| 15 |
@st.cache_resource
|
| 16 |
def get_chatbot():
|
| 17 |
+
# Auto-create knowledge base
|
| 18 |
if not os.path.exists("knowledge.txt"):
|
| 19 |
with open("knowledge.txt", "w") as f:
|
| 20 |
f.write("""
|
| 21 |
SR University is located in Warangal, Telangana, India.
|
| 22 |
+
Computer Science program focuses on AI/ML, DSA, Java/Python, AWS/Azure, software engineering.
|
|
|
|
| 23 |
|
| 24 |
+
B.Tech student preparing for AI/ML internships. Skills: LeetCode, AI projects (robotic arms, drones),
|
|
|
|
| 25 |
cloud certifications, competitive programming.
|
| 26 |
|
| 27 |
+
Internship tips:
|
| 28 |
+
1. 300+ LeetCode (Easy:100, Medium:150, Hard:50)
|
| 29 |
+
2. 3 portfolio projects: RAG chatbot, object detection, RL agent
|
| 30 |
+
3. Apply startups: AngelList, Y Combinator
|
| 31 |
+
4. Practice system design, behavioral interviews
|
| 32 |
+
5. Target: Google, Microsoft, Hyderabad/Bangalore startups
|
| 33 |
""")
|
| 34 |
|
| 35 |
+
# Create FAISS index if missing
|
| 36 |
if not os.path.exists("faiss_index"):
|
| 37 |
loader = TextLoader("knowledge.txt")
|
| 38 |
docs = loader.load()
|
|
|
|
| 48 |
vectorstore = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 49 |
retriever = vectorstore.as_retriever()
|
| 50 |
|
| 51 |
+
# LLM
|
| 52 |
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
|
|
|
| 53 |
prompt = ChatPromptTemplate.from_template(
|
| 54 |
+
"Answer using ONLY this context:\n{context}\n\nQuestion: {question}\nAnswer:"
|
| 55 |
)
|
| 56 |
|
| 57 |
def rag_chain(query):
|
|
|
|
| 59 |
context = "\n".join([doc.page_content for doc in context_docs])
|
| 60 |
chain = (
|
| 61 |
{"context": lambda x: context, "question": lambda x: query}
|
| 62 |
+
| prompt | llm | StrOutputParser()
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
return chain.invoke(query)
|
| 65 |
|
| 66 |
return rag_chain
|
| 67 |
|
|
|
|
| 68 |
st.title("🧠 RAG Chatbot")
|
| 69 |
+
st.info("💡 Ask about SR University, AI internships, projects...")
|
| 70 |
|
| 71 |
chatbot = get_chatbot()
|
|
|
|
| 72 |
if "messages" not in st.session_state:
|
| 73 |
st.session_state.messages = []
|
| 74 |
|
|
|
|
| 75 |
for message in st.session_state.messages:
|
| 76 |
with st.chat_message(message["role"]):
|
| 77 |
st.markdown(message["content"])
|
| 78 |
|
| 79 |
+
if prompt := st.chat_input("Ask a question..."):
|
|
|
|
|
|
|
| 80 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 81 |
with st.chat_message("user"):
|
| 82 |
st.markdown(prompt)
|
| 83 |
|
|
|
|
| 84 |
with st.chat_message("assistant"):
|
| 85 |
+
with st.spinner("Thinking..."):
|
| 86 |
response = chatbot(prompt)
|
| 87 |
st.markdown(response)
|
| 88 |
|
|
|
|
| 89 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|