Trinay16's picture
Update src/chatbot.py
87641f3 verified
import streamlit as st
import requests
import re
import os
HF_API_URLtoken = os.getenv("HF_API_TOKEN")
HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
def query_huggingface(prompt):
headers = {
"Authorization": f"Bearer {HF_API_TOKEN}",
"Content-Type": "application/json"
}
payload = {
"inputs": prompt,
"parameters": {"max_new_tokens": 150}
}
response = requests.post(HF_API_URL, headers=headers, json=payload)
try:
return response.json()[0]["generated_text"]
except Exception:
return "Sorry, I couldn't process that message."
def clean_bot_response(full_response, prompt):
response = full_response.replace(prompt, "")
cleanup_phrases = [
"You are an assistant for Stick Cricket Showdown.",
"Answer:",
"Bot:",
prompt
]
for phrase in cleanup_phrases:
response = response.replace(phrase, "")
return re.sub(r"\n+", "\n", response).strip()
def generate_prompt(user_input):
return f"""
You are an assistant for the Stick Cricket Showdown app. Answer as if you're part of the development team.
App Overview:
- A Streamlit-based cricket simulation tool with real-time scoring, team setup, and post-match analytics.
- Developed by Team_Karthik with modules for player creation, live match, scorecard, and match setup.
Team Roles:
- Karthik: Scorecard.py
- Trinay: Match_setup.py
- Phanindra: Create_Player.py, animations
- Shadvikram: Live_Match.py
- Rishi: Homepage.py & navigation
Tech Stack:
- Python, Streamlit, Lottie animations
User Feedback:
- Users love animations and gameplay
- Suggestions include commentary, real player stats, over-based play
You are now chatting with a user. They asked:
'{user_input}'
Respond in a friendly, knowledgeable tone.
"""
def render_chatbot(movie_title: str):
if st.session_state.get("suppress_chatbot"):
return
with st.sidebar:
st.markdown("## Stick Cricket Showdown Chatbot")
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
user_input = st.text_input("Ask me anything about the app:", key=f"chat_input_{movie_title}")
if st.button("Send", key=f"send_button_{movie_title}"):
if user_input:
prompt = generate_prompt(user_input)
raw_response = query_huggingface(prompt)
clean_response = clean_bot_response(raw_response, prompt)
st.session_state.chat_history.append(f"**You:** {user_input}\n\n**Bot:** {clean_response}")
for a in reversed(st.session_state.chat_history):
st.markdown(a)
if "feedback" not in st.session_state:
st.session_state.feedback = []
st.text_area("Share any feedback:", key="feedback_text")
if st.button("Submit Feedback"):
if st.session_state["feedback_text"]:
st.session_state.feedback.append(st.session_state["feedback_text"])
st.success("Thanks for your feedback!")