File size: 3,098 Bytes
e852957
 
 
 
87641f3
e852957
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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!")