Spaces:
Running
Running
import streamlit as st | |
from groq import Groq | |
# Get the API key from Streamlit secrets | |
API_KEY = st.secrets["GROQ_API_KEY"] | |
# Check if API key is loaded successfully | |
if not API_KEY: | |
st.error("API key not found. Please set GROQ_API_KEY in Streamlit secrets.") | |
st.stop() | |
# Template for instruction on extracting data with commentary | |
template = ( | |
"You are tasked with reviewing a chess game in PGN format: {pgn_content}. " | |
"Please follow these instructions carefully:\n\n" | |
"1. Game Summary:\n" | |
" - Wow! What a battle! Provide an exciting summary of the game in detail, capturing critical moments, major blunders, and brilliant tactical sequences. " | |
"Make it feel like a thrilling chess match analysis, bringing out the intensity and strategic brilliance of both players.\n\n" | |
"2. Move-by-Move Review:\n" | |
" - Review each move with excitement and classify it as 'Brilliant', 'Good', 'Average', 'Mistake', or 'Blunder'. " | |
"Use fun and engaging commentary to highlight critical moments. Examples:\n" | |
" - 'Whoa! What a stunning move! This changes everything!' (Brilliant)\n" | |
" - 'A solid and logical play, keeping control of the board.' (Good)\n" | |
" - 'Hmm, not the worst, but could have been sharper!' (Average)\n" | |
" - 'Oops! That was a slip-up—this could spell trouble.' (Mistake)\n" | |
" - 'Oh no! A complete disaster! This could be the turning point!' (Blunder)\n" | |
" - Clearly mention the player who made the move and why it fits the category.\n\n" | |
"3. Player Ratings:\n" | |
" - Give each player a performance score out of 10, based on their gameplay. Be honest but engaging! Examples:\n" | |
" - 'A masterclass in precision—9/10!'\n" | |
" - 'Decent play, but missed some key chances—6/10.'\n" | |
" - 'Tough game! Some serious room for improvement—3/10.'\n\n" | |
"4. Phase Classification:\n" | |
" - Label every move as part of the opening, middlegame, or endgame. Highlight major transitions, such as:\n" | |
" - 'And now, we transition into the fiery middlegame—where the real battle begins!'\n" | |
" - 'Endgame time! Every move now is make-or-break!'\n\n" | |
"5. Recommendations for Improvement:\n" | |
" - Provide insightful and engaging advice to help players sharpen their skills. Examples:\n" | |
" - 'Next time, try controlling the center early on—it makes all the difference!'\n" | |
" - 'Watch out for those sneaky forks! Tactical awareness is key!'\n" | |
" - 'Your endgame technique needs work—study king and pawn endings!'\n\n" | |
"6. Mistake Evaluation:\n" | |
" - Identify the biggest blunder for each player and explain the consequences dramatically. Examples:\n" | |
" - 'Oh no! This was the moment everything fell apart!' \n" | |
" - 'A devastating oversight—this move turned victory into defeat!'\n" | |
" - 'Yikes! That was a tactical disaster—missed a winning sequence!'\n\n" | |
"7. Phase Review:\n" | |
" - Give a colorful review of how each player handled the opening, middlegame, and endgame. Examples:\n" | |
" - 'The opening was a solid display of fundamentals, but things got wild in the middlegame!'\n" | |
" - 'Brilliant endgame technique! Like a seasoned pro!' \n" | |
" - 'Struggled in the middlegame—got lost in the complexity!'\n\n" | |
"8. Phase Ratings:\n" | |
" - Score players based on their performance in each phase. Examples:\n" | |
" - 'Opening: 7/10 – Strong start, but could use more preparation.'\n" | |
" - 'Middlegame: 4/10 – Some great ideas but too many missed opportunities!'\n" | |
" - 'Endgame: 9/10 – Clinical finish! Well played!'\n\n" | |
"9. Formatting Rules:\n" | |
" - Keep it fun and energetic—no robotic analysis!\n" | |
" - If the game content is invalid or unclear, return an empty string ('').\n" | |
" - Address each player by their username—no pronouns!\n" | |
) | |
# Function to interact with the Groq API | |
def review_chess_game(pgn_content): | |
# Initialize the Groq client with API key | |
client = Groq(api_key=API_KEY) | |
# Format the template with the PGN content | |
prompt = template.format(pgn_content=pgn_content) | |
try: | |
# Send request to the Groq API using the formatted prompt | |
completion = client.chat.completions.create( | |
model="llama-3.3-70b-versatile", # Groq model choice | |
messages=[{"role": "user", "content": prompt}], | |
temperature=1, | |
max_tokens=4096, | |
top_p=1, | |
stream=True, | |
stop=None, | |
) | |
# Collect streaming response progressively | |
response_chunks = [] | |
for message in completion: | |
chunk = message.choices[0].delta.content or "" | |
response_chunks.append(chunk) # Append each chunk to the list | |
# Join all chunks into a complete response | |
full_response = "".join(response_chunks) | |
return full_response.strip() | |
except Exception as e: | |
return f"Error processing PGN: {e}" | |
# Streamlit App | |
st.title("Chess Game Review") | |
# Upload PGN file | |
uploaded_file = st.file_uploader("Upload a PGN file", type="pgn") | |
if uploaded_file is not None: | |
# Read the PGN content from the uploaded file | |
pgn_content = uploaded_file.read().decode("utf-8") | |
# Display the PGN content | |
st.subheader("PGN Content") | |
st.text(pgn_content) | |
# Review the chess game | |
st.subheader("Game Review") | |
with st.spinner("Analyzing the game..."): | |
result = review_chess_game(pgn_content) | |
st.write(result) |