Spaces:
Sleeping
Sleeping
IamHussain
commited on
Commit
·
892f323
1
Parent(s):
307771a
Added Flask API for score submission
Browse files- app.py +42 -24
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -1,22 +1,42 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
from
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
| 7 |
submitted_scores = []
|
| 8 |
|
| 9 |
-
|
| 10 |
-
def submit_score(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def calculate_top_miners():
|
| 21 |
miner_scores = {}
|
| 22 |
for entry in submitted_scores:
|
|
@@ -30,30 +50,28 @@ def calculate_top_miners():
|
|
| 30 |
miner_scores[miner_id]["HNR"] += score["HNR"]
|
| 31 |
miner_scores[miner_id]["CLAP"] += score["CLAP"]
|
| 32 |
miner_scores[miner_id]["count"] += 1
|
| 33 |
-
|
| 34 |
-
# Calculate average score
|
| 35 |
for miner_id, scores in miner_scores.items():
|
| 36 |
scores["SNR"] /= scores["count"]
|
| 37 |
scores["HNR"] /= scores["count"]
|
| 38 |
scores["CLAP"] /= scores["count"]
|
| 39 |
|
| 40 |
-
# Sort
|
| 41 |
sorted_miners = sorted(miner_scores.items(), key=lambda x: (x[1]["SNR"] + x[1]["HNR"] + x[1]["CLAP"]), reverse=True)
|
| 42 |
-
|
| 43 |
-
return sorted_miners[:10] # Return top 10 miners
|
| 44 |
|
| 45 |
-
# Streamlit UI
|
| 46 |
-
st.title("Top 10 Miner Scores")
|
| 47 |
-
|
| 48 |
-
# Display top 10 miners
|
| 49 |
if st.button("Display Top 10 Scores"):
|
| 50 |
top_miners = calculate_top_miners()
|
| 51 |
st.write("Top 10 Miners based on aggregated scores:")
|
| 52 |
|
| 53 |
-
# Prepare data for
|
| 54 |
miner_data = []
|
| 55 |
for miner, scores in top_miners:
|
| 56 |
miner_data.append([miner, scores["SNR"], scores["HNR"], scores["CLAP"]])
|
| 57 |
|
| 58 |
df = pd.DataFrame(miner_data, columns=["Miner ID", "SNR", "HNR", "CLAP"])
|
| 59 |
st.table(df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
from datetime import datetime
|
| 5 |
|
| 6 |
+
# Initialize a Flask app to handle POST requests
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# In-memory storage for scores
|
| 10 |
submitted_scores = []
|
| 11 |
|
| 12 |
+
@app.route('/submit_score', methods=['POST'])
|
| 13 |
+
def submit_score():
|
| 14 |
+
try:
|
| 15 |
+
# Extract data from the incoming JSON request
|
| 16 |
+
data = request.get_json()
|
| 17 |
+
validator_id = data['validator_id']
|
| 18 |
+
miner_id = data['miner_id']
|
| 19 |
+
score = data['score']
|
| 20 |
+
model_type = data['model_type']
|
| 21 |
+
timestamp = data['timestamp']
|
| 22 |
+
|
| 23 |
+
# Store the submitted score in memory
|
| 24 |
+
submitted_scores.append({
|
| 25 |
+
"validator_id": validator_id,
|
| 26 |
+
"miner_id": miner_id,
|
| 27 |
+
"score": score,
|
| 28 |
+
"model_type": model_type,
|
| 29 |
+
"timestamp": timestamp
|
| 30 |
+
})
|
| 31 |
+
|
| 32 |
+
return jsonify({"message": "Score submitted successfully"}), 200
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return jsonify({"error": str(e)}), 400
|
| 35 |
+
|
| 36 |
+
# Run the Streamlit app
|
| 37 |
+
st.title("Top 10 Miner Scores")
|
| 38 |
+
|
| 39 |
+
# Function to calculate top miners
|
| 40 |
def calculate_top_miners():
|
| 41 |
miner_scores = {}
|
| 42 |
for entry in submitted_scores:
|
|
|
|
| 50 |
miner_scores[miner_id]["HNR"] += score["HNR"]
|
| 51 |
miner_scores[miner_id]["CLAP"] += score["CLAP"]
|
| 52 |
miner_scores[miner_id]["count"] += 1
|
| 53 |
+
|
|
|
|
| 54 |
for miner_id, scores in miner_scores.items():
|
| 55 |
scores["SNR"] /= scores["count"]
|
| 56 |
scores["HNR"] /= scores["count"]
|
| 57 |
scores["CLAP"] /= scores["count"]
|
| 58 |
|
| 59 |
+
# Sort and return top 10 miners
|
| 60 |
sorted_miners = sorted(miner_scores.items(), key=lambda x: (x[1]["SNR"] + x[1]["HNR"] + x[1]["CLAP"]), reverse=True)
|
| 61 |
+
return sorted_miners[:10]
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
if st.button("Display Top 10 Scores"):
|
| 64 |
top_miners = calculate_top_miners()
|
| 65 |
st.write("Top 10 Miners based on aggregated scores:")
|
| 66 |
|
| 67 |
+
# Prepare data for table
|
| 68 |
miner_data = []
|
| 69 |
for miner, scores in top_miners:
|
| 70 |
miner_data.append([miner, scores["SNR"], scores["HNR"], scores["CLAP"]])
|
| 71 |
|
| 72 |
df = pd.DataFrame(miner_data, columns=["Miner ID", "SNR", "HNR", "CLAP"])
|
| 73 |
st.table(df)
|
| 74 |
+
|
| 75 |
+
# This is necessary to run Flask alongside Streamlit on Hugging Face
|
| 76 |
+
if __name__ == '__main__':
|
| 77 |
+
app.run(port=8501, host="0.0.0.0")
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
flask
|