Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from groq import Groq
|
| 3 |
+
|
| 4 |
+
# Get the API key from Streamlit secrets
|
| 5 |
+
API_KEY = st.secrets["GROQ_API_KEY"]
|
| 6 |
+
|
| 7 |
+
# Check if API key is loaded successfully
|
| 8 |
+
if not API_KEY:
|
| 9 |
+
st.error("API key not found. Please set GROQ_API_KEY in Streamlit secrets.")
|
| 10 |
+
st.stop()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Template for instruction on extracting data with commentary
|
| 14 |
+
template = (
|
| 15 |
+
"You are tasked with reviewing a chess game in PGN format: {pgn_content}. "
|
| 16 |
+
"Please follow these instructions carefully:\n\n"
|
| 17 |
+
|
| 18 |
+
"1. Game Summary:\n"
|
| 19 |
+
" - Wow! What a battle! Provide an exciting summary of the game in detail, capturing critical moments, major blunders, and brilliant tactical sequences. "
|
| 20 |
+
"Make it feel like a thrilling chess match analysis, bringing out the intensity and strategic brilliance of both players.\n\n"
|
| 21 |
+
|
| 22 |
+
"2. Move-by-Move Review:\n"
|
| 23 |
+
" - Review each move with excitement and classify it as 'Brilliant', 'Good', 'Average', 'Mistake', or 'Blunder'. "
|
| 24 |
+
"Use fun and engaging commentary to highlight critical moments. Examples:\n"
|
| 25 |
+
" - 'Whoa! What a stunning move! This changes everything!' (Brilliant)\n"
|
| 26 |
+
" - 'A solid and logical play, keeping control of the board.' (Good)\n"
|
| 27 |
+
" - 'Hmm, not the worst, but could have been sharper!' (Average)\n"
|
| 28 |
+
" - 'Oops! That was a slip-up—this could spell trouble.' (Mistake)\n"
|
| 29 |
+
" - 'Oh no! A complete disaster! This could be the turning point!' (Blunder)\n"
|
| 30 |
+
" - Clearly mention the player who made the move and why it fits the category.\n\n"
|
| 31 |
+
|
| 32 |
+
"3. Player Ratings:\n"
|
| 33 |
+
" - Give each player a performance score out of 10, based on their gameplay. Be honest but engaging! Examples:\n"
|
| 34 |
+
" - 'A masterclass in precision—9/10!'\n"
|
| 35 |
+
" - 'Decent play, but missed some key chances—6/10.'\n"
|
| 36 |
+
" - 'Tough game! Some serious room for improvement—3/10.'\n\n"
|
| 37 |
+
|
| 38 |
+
"4. Phase Classification:\n"
|
| 39 |
+
" - Label every move as part of the opening, middlegame, or endgame. Highlight major transitions, such as:\n"
|
| 40 |
+
" - 'And now, we transition into the fiery middlegame—where the real battle begins!'\n"
|
| 41 |
+
" - 'Endgame time! Every move now is make-or-break!'\n\n"
|
| 42 |
+
|
| 43 |
+
"5. Recommendations for Improvement:\n"
|
| 44 |
+
" - Provide insightful and engaging advice to help players sharpen their skills. Examples:\n"
|
| 45 |
+
" - 'Next time, try controlling the center early on—it makes all the difference!'\n"
|
| 46 |
+
" - 'Watch out for those sneaky forks! Tactical awareness is key!'\n"
|
| 47 |
+
" - 'Your endgame technique needs work—study king and pawn endings!'\n\n"
|
| 48 |
+
|
| 49 |
+
"6. Mistake Evaluation:\n"
|
| 50 |
+
" - Identify the biggest blunder for each player and explain the consequences dramatically. Examples:\n"
|
| 51 |
+
" - 'Oh no! This was the moment everything fell apart!' \n"
|
| 52 |
+
" - 'A devastating oversight—this move turned victory into defeat!'\n"
|
| 53 |
+
" - 'Yikes! That was a tactical disaster—missed a winning sequence!'\n\n"
|
| 54 |
+
|
| 55 |
+
"7. Phase Review:\n"
|
| 56 |
+
" - Give a colorful review of how each player handled the opening, middlegame, and endgame. Examples:\n"
|
| 57 |
+
" - 'The opening was a solid display of fundamentals, but things got wild in the middlegame!'\n"
|
| 58 |
+
" - 'Brilliant endgame technique! Like a seasoned pro!' \n"
|
| 59 |
+
" - 'Struggled in the middlegame—got lost in the complexity!'\n\n"
|
| 60 |
+
|
| 61 |
+
"8. Phase Ratings:\n"
|
| 62 |
+
" - Score players based on their performance in each phase. Examples:\n"
|
| 63 |
+
" - 'Opening: 7/10 – Strong start, but could use more preparation.'\n"
|
| 64 |
+
" - 'Middlegame: 4/10 – Some great ideas but too many missed opportunities!'\n"
|
| 65 |
+
" - 'Endgame: 9/10 – Clinical finish! Well played!'\n\n"
|
| 66 |
+
|
| 67 |
+
"9. Formatting Rules:\n"
|
| 68 |
+
" - Keep it fun and energetic—no robotic analysis!\n"
|
| 69 |
+
" - If the game content is invalid or unclear, return an empty string ('').\n"
|
| 70 |
+
" - Address each player by their username—no pronouns!\n"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Function to interact with the Groq API
|
| 74 |
+
def review_chess_game(pgn_content):
|
| 75 |
+
# Initialize the Groq client with API key
|
| 76 |
+
client = Groq(api_key=API_KEY)
|
| 77 |
+
|
| 78 |
+
# Format the template with the PGN content
|
| 79 |
+
prompt = template.format(pgn_content=pgn_content)
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
# Send request to the Groq API using the formatted prompt
|
| 83 |
+
completion = client.chat.completions.create(
|
| 84 |
+
model="llama-3.3-70b-versatile", # Groq model choice
|
| 85 |
+
messages=[{"role": "user", "content": prompt}],
|
| 86 |
+
temperature=1,
|
| 87 |
+
max_tokens=4096,
|
| 88 |
+
top_p=1,
|
| 89 |
+
stream=True,
|
| 90 |
+
stop=None,
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Collect streaming response progressively
|
| 94 |
+
response_chunks = []
|
| 95 |
+
for message in completion:
|
| 96 |
+
chunk = message.choices[0].delta.content or ""
|
| 97 |
+
response_chunks.append(chunk) # Append each chunk to the list
|
| 98 |
+
|
| 99 |
+
# Join all chunks into a complete response
|
| 100 |
+
full_response = "".join(response_chunks)
|
| 101 |
+
return full_response.strip()
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return f"Error processing PGN: {e}"
|
| 105 |
+
|
| 106 |
+
# Streamlit App
|
| 107 |
+
st.title("Chess Game Review with Groq API")
|
| 108 |
+
|
| 109 |
+
# Upload PGN file
|
| 110 |
+
uploaded_file = st.file_uploader("Upload a PGN file", type="pgn")
|
| 111 |
+
|
| 112 |
+
if uploaded_file is not None:
|
| 113 |
+
# Read the PGN content from the uploaded file
|
| 114 |
+
pgn_content = uploaded_file.read().decode("utf-8")
|
| 115 |
+
|
| 116 |
+
# Display the PGN content
|
| 117 |
+
st.subheader("PGN Content")
|
| 118 |
+
st.text(pgn_content)
|
| 119 |
+
|
| 120 |
+
# Review the chess game
|
| 121 |
+
st.subheader("Game Review")
|
| 122 |
+
with st.spinner("Analyzing the game..."):
|
| 123 |
+
result = review_chess_game(pgn_content)
|
| 124 |
+
st.write(result)
|