Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
import os
|
2 |
import re
|
3 |
-
from datetime import datetime
|
4 |
|
5 |
import gradio as gr
|
6 |
-
import
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
def get_evaluation_questions():
|
12 |
"""
|
@@ -37,77 +39,107 @@ def sanitize_input(text):
|
|
37 |
clean_text = re.sub(r"[^a-zA-Z0-9\s.,!?@:\-]", "", text)
|
38 |
return clean_text.strip()[:500]
|
39 |
|
40 |
-
def
|
|
|
|
|
|
|
41 |
"""
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
Returns the evaluation results as a string.
|
47 |
"""
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
email = sanitize_input(email)
|
50 |
name = sanitize_input(name)
|
51 |
system_prompt = sanitize_input(system_prompt)
|
52 |
|
53 |
score = 0
|
54 |
responses = []
|
|
|
55 |
for item in EVALUATION_QUESTIONS:
|
56 |
question = item["question"]
|
57 |
expected = item["expected"]
|
58 |
try:
|
59 |
-
|
|
|
60 |
model="gpt-4o-mini", # Ensure this identifier matches the deployed model.
|
61 |
messages=[
|
62 |
{"role": "system", "content": system_prompt},
|
63 |
{"role": "user", "content": question}
|
64 |
]
|
65 |
)
|
66 |
-
answer
|
|
|
67 |
except Exception as e:
|
68 |
answer = f"Error during OpenAI API call: {str(e)}"
|
69 |
-
|
70 |
# Simple evaluation: check if the answer contains the expected substring.
|
71 |
if expected.lower() in answer.lower():
|
72 |
score += 1
|
73 |
verdict = "Correct"
|
74 |
else:
|
75 |
verdict = "Incorrect"
|
76 |
-
|
77 |
responses.append(
|
78 |
f"Question: {question}\n"
|
79 |
f"Answer: {answer}\n"
|
80 |
f"Expected: {expected}\n"
|
81 |
f"Result: {verdict}\n"
|
82 |
)
|
83 |
-
|
84 |
result_details = "\n".join(responses)
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
def build_interface():
|
89 |
"""
|
90 |
-
Constructs the Gradio interface.
|
91 |
"""
|
92 |
with gr.Blocks() as demo:
|
93 |
-
gr.Markdown("# GPT-4o Mini Prompt
|
94 |
-
gr.Markdown(
|
95 |
-
|
|
|
|
|
|
|
96 |
email_input = gr.Textbox(label="Email", placeholder="your.email@example.com")
|
97 |
name_input = gr.Textbox(label="Name", placeholder="Your name")
|
98 |
system_prompt_input = gr.Textbox(
|
99 |
label="System Prompt",
|
100 |
placeholder="Enter your system prompt here...",
|
101 |
-
lines=6
|
102 |
)
|
103 |
-
|
104 |
output_text = gr.Textbox(label="Results", lines=15)
|
105 |
-
|
106 |
-
|
107 |
-
fn=
|
108 |
inputs=[email_input, name_input, system_prompt_input],
|
109 |
-
outputs=output_text
|
110 |
)
|
|
|
111 |
return demo
|
112 |
|
113 |
if __name__ == "__main__":
|
|
|
1 |
import os
|
2 |
import re
|
|
|
3 |
|
4 |
import gradio as gr
|
5 |
+
from openai import OpenAI
|
6 |
|
7 |
+
# Initialize the OpenAI client with the API key from environment variables.
|
8 |
+
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
9 |
+
|
10 |
+
# In-memory storage to track submitted emails (not persistent; resets on app restart).
|
11 |
+
submitted_emails = set()
|
12 |
|
13 |
def get_evaluation_questions():
|
14 |
"""
|
|
|
39 |
clean_text = re.sub(r"[^a-zA-Z0-9\s.,!?@:\-]", "", text)
|
40 |
return clean_text.strip()[:500]
|
41 |
|
42 |
+
def validate_email(email):
|
43 |
+
"""
|
44 |
+
Validates that the provided email is in a valid format.
|
45 |
+
Returns True if valid, False otherwise.
|
46 |
"""
|
47 |
+
email_regex = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
|
48 |
+
return re.match(email_regex, email) is not None
|
49 |
+
|
50 |
+
def submit_prompt(email, name, system_prompt):
|
|
|
51 |
"""
|
52 |
+
Handles user submission:
|
53 |
+
- Validates email format.
|
54 |
+
- Checks if the email has already been used for submission.
|
55 |
+
- Evaluates the system prompt against predefined test questions.
|
56 |
+
- Prevents multiple submissions from the same email.
|
57 |
+
Returns the evaluation results or an error message if the submission is invalid.
|
58 |
+
"""
|
59 |
+
# Validate email format.
|
60 |
+
if not validate_email(email):
|
61 |
+
return "Invalid email address. Please enter a valid email."
|
62 |
+
|
63 |
+
# Check if this email has already been used for submission.
|
64 |
+
if email in submitted_emails:
|
65 |
+
return f"Submission already received for {email}. You can only submit once."
|
66 |
+
|
67 |
+
# Sanitize inputs.
|
68 |
email = sanitize_input(email)
|
69 |
name = sanitize_input(name)
|
70 |
system_prompt = sanitize_input(system_prompt)
|
71 |
|
72 |
score = 0
|
73 |
responses = []
|
74 |
+
|
75 |
for item in EVALUATION_QUESTIONS:
|
76 |
question = item["question"]
|
77 |
expected = item["expected"]
|
78 |
try:
|
79 |
+
# Use the new client-based API for chat completions.
|
80 |
+
response = client.chat.completions.create(
|
81 |
model="gpt-4o-mini", # Ensure this identifier matches the deployed model.
|
82 |
messages=[
|
83 |
{"role": "system", "content": system_prompt},
|
84 |
{"role": "user", "content": question}
|
85 |
]
|
86 |
)
|
87 |
+
# Extract the answer from the response object.
|
88 |
+
answer = response.choices[0].message.content.strip()
|
89 |
except Exception as e:
|
90 |
answer = f"Error during OpenAI API call: {str(e)}"
|
91 |
+
|
92 |
# Simple evaluation: check if the answer contains the expected substring.
|
93 |
if expected.lower() in answer.lower():
|
94 |
score += 1
|
95 |
verdict = "Correct"
|
96 |
else:
|
97 |
verdict = "Incorrect"
|
98 |
+
|
99 |
responses.append(
|
100 |
f"Question: {question}\n"
|
101 |
f"Answer: {answer}\n"
|
102 |
f"Expected: {expected}\n"
|
103 |
f"Result: {verdict}\n"
|
104 |
)
|
105 |
+
|
106 |
result_details = "\n".join(responses)
|
107 |
+
|
108 |
+
# Record this email as having submitted their prompt.
|
109 |
+
submitted_emails.add(email)
|
110 |
+
|
111 |
+
return (
|
112 |
+
f"Thank you for your submission, {name}!\n\n"
|
113 |
+
f"Your evaluation score is {score} out of {len(EVALUATION_QUESTIONS)}.\n\nDetails:\n{result_details}"
|
114 |
+
)
|
115 |
|
116 |
def build_interface():
|
117 |
"""
|
118 |
+
Constructs the Gradio interface with a submission button and single-submission mechanism.
|
119 |
"""
|
120 |
with gr.Blocks() as demo:
|
121 |
+
gr.Markdown("# GPT-4o Mini Prompt Submission")
|
122 |
+
gr.Markdown(
|
123 |
+
"Please enter your details and submit your system prompt below. "
|
124 |
+
"You can only submit once."
|
125 |
+
)
|
126 |
+
|
127 |
email_input = gr.Textbox(label="Email", placeholder="your.email@example.com")
|
128 |
name_input = gr.Textbox(label="Name", placeholder="Your name")
|
129 |
system_prompt_input = gr.Textbox(
|
130 |
label="System Prompt",
|
131 |
placeholder="Enter your system prompt here...",
|
132 |
+
lines=6,
|
133 |
)
|
134 |
+
submit_button = gr.Button("Submit")
|
135 |
output_text = gr.Textbox(label="Results", lines=15)
|
136 |
+
|
137 |
+
submit_button.click(
|
138 |
+
fn=submit_prompt,
|
139 |
inputs=[email_input, name_input, system_prompt_input],
|
140 |
+
outputs=output_text,
|
141 |
)
|
142 |
+
|
143 |
return demo
|
144 |
|
145 |
if __name__ == "__main__":
|