Spaces:
Runtime error
Runtime error
File size: 2,644 Bytes
478d778 f1a291d |
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 |
import os
import random
import csv
import requests
from sentence_transformers import SentenceTransformer
import gradio as gr
with open("secrets.csv", 'w') as file:
file.write(os.getenv('secrets'))
# Load the CSV file
csv_file = "secrets.csv"
questions = []
answers = []
with open(csv_file, "r") as file:
reader = csv.DictReader(file)
for row in reader:
questions.append(row["questions"])
answers.append(row["answers"])
# Initialize the SentenceTransformer model
model = SentenceTransformer("BAAI/bge-base-en-v1.5")
def predict_prompt(name, email):
# Select 10 random answers from the CSV file
selected_answers = random.sample(answers, 10)
total_score = 0
results = []
# Iterate over the selected answers
for i, answer in enumerate(selected_answers, 1):
actual_question = questions[answers.index(answer)]
# Prompt the user to enter their predicted question
predicted_question = gr.components.Textbox(label=f"Answer {i}: {answer}")
# Encode the predicted and actual questions
predicted = model.encode(predicted_question, normalize_embeddings=True)
actual = model.encode(actual_question, normalize_embeddings=True)
# Calculate the similarity score
similarity = predicted @ actual.T
score = float(similarity)
results.append(f"Answer {i}: {answer}\nPredicted Question: {predicted_question}\nScore: {score}\n")
total_score += score
results.append(f"\nTotal Score: {total_score}")
# Make an API call to submit the user's name, email, and score
url = "https://api.example.com/submit" # Replace with the actual API endpoint URL
data = {
"name": name,
"email": email,
"total_score": total_score
}
response = requests.post(url, json=data)
if response.status_code == 200:
results.append("Score submitted successfully!")
else:
results.append("Failed to submit the score.")
return "\n".join(results)
# Create the Gradio interface
iface = gr.Interface(
fn=predict_prompt,
inputs=[
gr.components.Textbox(label="Name"),
gr.components.Textbox(label="Email")
],
outputs="text",
title="Predict the Prompt",
description="Enter your name and email to start the competition. You will be presented with 10 random answers, and your task is to predict the question for each answer. Your score will be calculated based on the similarity between your predicted question and the actual question.",
)
# Launch the Gradio app
iface.launch() |