IamHussain
Added Flask API for score submission
2e4c567
import streamlit as st
import pandas as pd
from flask import Flask, request, jsonify
from datetime import datetime
# Initialize a Flask app to handle POST requests
app = Flask(__name__)
# In-memory storage for scores
submitted_scores = []
@app.route('/submit_score', methods=['POST'])
def submit_score():
try:
# Extract data from the incoming JSON request
data = request.get_json()
validator_id = data['validator_id']
miner_id = data['miner_id']
score = data['score']
model_type = data['model_type']
timestamp = data['timestamp']
# Store the submitted score in memory
submitted_scores.append({
"validator_id": validator_id,
"miner_id": miner_id,
"score": score,
"model_type": model_type,
"timestamp": timestamp
})
return jsonify({"message": "Score submitted successfully"}), 200
except Exception as e:
return jsonify({"error": str(e)}), 400
# Run the Streamlit app
st.title("Top 10 Miner Scores")
# Function to calculate top miners
def calculate_top_miners():
miner_scores = {}
for entry in submitted_scores:
miner_id = entry["miner_id"]
score = entry["score"]
if miner_id not in miner_scores:
miner_scores[miner_id] = {"SNR": 0, "HNR": 0, "CLAP": 0, "count": 0}
miner_scores[miner_id]["SNR"] += score["SNR"]
miner_scores[miner_id]["HNR"] += score["HNR"]
miner_scores[miner_id]["CLAP"] += score["CLAP"]
miner_scores[miner_id]["count"] += 1
for miner_id, scores in miner_scores.items():
scores["SNR"] /= scores["count"]
scores["HNR"] /= scores["count"]
scores["CLAP"] /= scores["count"]
# Sort and return top 10 miners
sorted_miners = sorted(miner_scores.items(), key=lambda x: (x[1]["SNR"] + x[1]["HNR"] + x[1]["CLAP"]), reverse=True)
return sorted_miners[:10]
if st.button("Display Top 10 Scores"):
top_miners = calculate_top_miners()
st.write("Top 10 Miners based on aggregated scores:")
# Prepare data for table
miner_data = []
for miner, scores in top_miners:
miner_data.append([miner, scores["SNR"], scores["HNR"], scores["CLAP"]])
df = pd.DataFrame(miner_data, columns=["Miner ID", "SNR", "HNR", "CLAP"])
st.table(df)
# This is necessary to run Flask alongside Streamlit on Hugging Face
if __name__ == '__main__':
app.run(port=8501, host="0.0.0.0")