Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +136 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import os to handle environment variables
|
| 2 |
+
#
|
| 3 |
+
# To run this app
|
| 4 |
+
#
|
| 5 |
+
# streamlit run <path to this file, for example, rag\streamlit_app_basic.py>
|
| 6 |
+
#
|
| 7 |
+
import os
|
| 8 |
+
import uuid
|
| 9 |
+
import datetime
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
import streamlit as st
|
| 12 |
+
from openai import AzureOpenAI
|
| 13 |
+
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
# --- Session State Initialization ---
|
| 17 |
+
if "conversations" not in st.session_state:
|
| 18 |
+
st.session_state.conversations = {}
|
| 19 |
+
|
| 20 |
+
if "current_conversation_id" not in st.session_state:
|
| 21 |
+
conversation_id = str(uuid.uuid4())
|
| 22 |
+
st.session_state.conversations[conversation_id] = {
|
| 23 |
+
"created_at": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
|
| 24 |
+
"messages": [
|
| 25 |
+
{"role": "system", "content":
|
| 26 |
+
"""You are a helpful AI assistant which answers questions about cricket and sports in general.
|
| 27 |
+
Do not answer questions about other topics.
|
| 28 |
+
If you do not know the answer, say 'I do not know the answer to that question.'"""
|
| 29 |
+
}
|
| 30 |
+
]
|
| 31 |
+
}
|
| 32 |
+
st.session_state.current_conversation_id = conversation_id
|
| 33 |
+
|
| 34 |
+
def get_current_conversation():
|
| 35 |
+
return st.session_state.conversations[st.session_state.current_conversation_id]["messages"]
|
| 36 |
+
|
| 37 |
+
def generate_response(input_text):
|
| 38 |
+
client = AzureOpenAI(
|
| 39 |
+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
|
| 40 |
+
api_key=os.getenv("AZURE_OPENAI_KEY"),
|
| 41 |
+
api_version=os.getenv("AZURE_OPENAI_API_VERSION")
|
| 42 |
+
)
|
| 43 |
+
model_name = os.getenv("AZURE_OPENAI_MODEL_NAME")
|
| 44 |
+
try:
|
| 45 |
+
current_messages = get_current_conversation()
|
| 46 |
+
current_messages.append({"role": "user", "content": input_text})
|
| 47 |
+
with st.spinner("Generating Answer. Please wait..."):
|
| 48 |
+
response = client.chat.completions.create(
|
| 49 |
+
model=model_name,
|
| 50 |
+
messages=current_messages
|
| 51 |
+
)
|
| 52 |
+
answer = response.choices[0].message.content.strip()
|
| 53 |
+
current_messages.append({"role": "assistant", "content": answer})
|
| 54 |
+
return answer
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"Error: {e}")
|
| 57 |
+
st.error("An error occurred while processing your request. Please check your Azure configuration.")
|
| 58 |
+
return None
|
| 59 |
+
|
| 60 |
+
def create_new_conversation():
|
| 61 |
+
conversation_id = str(uuid.uuid4())
|
| 62 |
+
st.session_state.conversations[conversation_id] = {
|
| 63 |
+
"created_at": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
|
| 64 |
+
"messages": [
|
| 65 |
+
{"role": "system", "content":
|
| 66 |
+
"""You are a helpful AI assistant which answers questions about cricket and sports in general.
|
| 67 |
+
Do not answer questions about other topics.
|
| 68 |
+
If you do not know the answer, say 'I do not know the answer to that question.'"""
|
| 69 |
+
}
|
| 70 |
+
]
|
| 71 |
+
}
|
| 72 |
+
st.session_state.current_conversation_id = conversation_id
|
| 73 |
+
st.session_state.user_question = "" # Clear the input box
|
| 74 |
+
|
| 75 |
+
# --- Sidebar: Show only session titles ---
|
| 76 |
+
st.sidebar.title("Conversation Sessions")
|
| 77 |
+
for conv_id, conv_data in st.session_state.conversations.items():
|
| 78 |
+
# Use first user message as title, else fallback to timestamp
|
| 79 |
+
title = "New Conversation"
|
| 80 |
+
for msg in conv_data["messages"]:
|
| 81 |
+
if msg["role"] == "user":
|
| 82 |
+
title = msg["content"][:30] + "..." if len(msg["content"]) > 30 else msg["content"]
|
| 83 |
+
break
|
| 84 |
+
if st.sidebar.button(title, key=f"conv_{conv_id}"):
|
| 85 |
+
st.session_state.current_conversation_id = conv_id
|
| 86 |
+
|
| 87 |
+
# --- Main Page Layout ---
|
| 88 |
+
col1, col2 = st.columns([8, 1], gap="small")
|
| 89 |
+
with col2:
|
| 90 |
+
st.markdown(
|
| 91 |
+
"""
|
| 92 |
+
<style>
|
| 93 |
+
div.stButton > button {
|
| 94 |
+
width: 100%;
|
| 95 |
+
background-color: #2563eb;
|
| 96 |
+
color: white;
|
| 97 |
+
font-weight: bold;
|
| 98 |
+
border-radius: 6px;
|
| 99 |
+
padding: 0.5em 0.8em;
|
| 100 |
+
font-size: 1.1em;
|
| 101 |
+
}
|
| 102 |
+
</style>
|
| 103 |
+
""",
|
| 104 |
+
unsafe_allow_html=True,
|
| 105 |
+
)
|
| 106 |
+
st.button("🆕 New Chat", key="new_chat_btn", help="Start a new conversation", use_container_width=True, on_click=create_new_conversation)
|
| 107 |
+
|
| 108 |
+
st.markdown("<h1 style='text-align: center; color: #2563eb;'>Darshit's Chatbot 🤖</h1>", unsafe_allow_html=True)
|
| 109 |
+
st.markdown("---")
|
| 110 |
+
|
| 111 |
+
# --- Display Current Conversation ---
|
| 112 |
+
chat_container = st.container()
|
| 113 |
+
with chat_container:
|
| 114 |
+
for message in get_current_conversation():
|
| 115 |
+
if message["role"] == "user":
|
| 116 |
+
st.markdown(
|
| 117 |
+
f"<div style='background-color:#e0e7ff; border-radius:8px; padding:8px; margin-bottom:4px;'><b>You:</b> {message['content']}</div>",
|
| 118 |
+
unsafe_allow_html=True,
|
| 119 |
+
)
|
| 120 |
+
elif message["role"] == "assistant":
|
| 121 |
+
st.markdown(
|
| 122 |
+
f"<div style='background-color:#f1f5f9; border-radius:8px; padding:8px; margin-bottom:8px;'><b>Bot:</b> {message['content']}</div>",
|
| 123 |
+
unsafe_allow_html=True,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# --- User Input ---
|
| 127 |
+
st.markdown("---")
|
| 128 |
+
user_question = st.text_input(
|
| 129 |
+
"Ask any question about cricket or sports (Press Enter to submit):",
|
| 130 |
+
key="user_question",
|
| 131 |
+
placeholder="Type your question here...",
|
| 132 |
+
)
|
| 133 |
+
if user_question:
|
| 134 |
+
answer = generate_response(user_question)
|
| 135 |
+
if answer:
|
| 136 |
+
st.info(answer)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-dotenv
|
| 2 |
+
streamlit
|
| 3 |
+
openai
|