Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import csv
|
3 |
+
import requests
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
with open("secrets.csv", 'w') as file:
|
8 |
+
file.write(os.getenv('secrets'))
|
9 |
+
|
10 |
+
# Load the CSV file
|
11 |
+
csv_file = "secrets.csv"
|
12 |
+
questions = []
|
13 |
+
answers = []
|
14 |
+
|
15 |
+
with open(csv_file, "r") as file:
|
16 |
+
reader = csv.DictReader(file)
|
17 |
+
for row in reader:
|
18 |
+
questions.append(row["questions"])
|
19 |
+
answers.append(row["answers"])
|
20 |
+
|
21 |
+
# Initialize the SentenceTransformer model
|
22 |
+
model = SentenceTransformer("BAAI/bge-base-en-v1.5")
|
23 |
+
|
24 |
+
def predict_prompt(name, email):
|
25 |
+
# Select 10 random answers from the CSV file
|
26 |
+
selected_answers = random.sample(answers, 10)
|
27 |
+
|
28 |
+
total_score = 0
|
29 |
+
results = []
|
30 |
+
|
31 |
+
# Iterate over the selected answers
|
32 |
+
for i, answer in enumerate(selected_answers, 1):
|
33 |
+
actual_question = questions[answers.index(answer)]
|
34 |
+
|
35 |
+
# Prompt the user to enter their predicted question
|
36 |
+
predicted_question = gr.components.Textbox(label=f"Answer {i}: {answer}")
|
37 |
+
|
38 |
+
# Encode the predicted and actual questions
|
39 |
+
predicted = model.encode(predicted_question, normalize_embeddings=True)
|
40 |
+
actual = model.encode(actual_question, normalize_embeddings=True)
|
41 |
+
|
42 |
+
# Calculate the similarity score
|
43 |
+
similarity = predicted @ actual.T
|
44 |
+
score = float(similarity)
|
45 |
+
|
46 |
+
results.append(f"Answer {i}: {answer}\nPredicted Question: {predicted_question}\nScore: {score}\n")
|
47 |
+
total_score += score
|
48 |
+
|
49 |
+
results.append(f"\nTotal Score: {total_score}")
|
50 |
+
|
51 |
+
# Make an API call to submit the user's name, email, and score
|
52 |
+
url = "https://api.example.com/submit" # Replace with the actual API endpoint URL
|
53 |
+
data = {
|
54 |
+
"name": name,
|
55 |
+
"email": email,
|
56 |
+
"total_score": total_score
|
57 |
+
}
|
58 |
+
|
59 |
+
response = requests.post(url, json=data)
|
60 |
+
|
61 |
+
if response.status_code == 200:
|
62 |
+
results.append("Score submitted successfully!")
|
63 |
+
else:
|
64 |
+
results.append("Failed to submit the score.")
|
65 |
+
|
66 |
+
return "\n".join(results)
|
67 |
+
|
68 |
+
# Create the Gradio interface
|
69 |
+
iface = gr.Interface(
|
70 |
+
fn=predict_prompt,
|
71 |
+
inputs=[
|
72 |
+
gr.components.Textbox(label="Name"),
|
73 |
+
gr.components.Textbox(label="Email")
|
74 |
+
],
|
75 |
+
outputs="text",
|
76 |
+
title="Predict the Prompt",
|
77 |
+
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.",
|
78 |
+
)
|
79 |
+
|
80 |
+
# Launch the Gradio app
|
81 |
+
iface.launch()
|