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()