Spaces:
Runtime error
Runtime error
Generate Button Behavior
Browse files
app.py
CHANGED
|
@@ -2,59 +2,64 @@ import gradio as gr
|
|
| 2 |
import time
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
generator = pipeline("text-generation", model="gpt2")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
paradata = {}
|
| 10 |
|
| 11 |
-
# Generate 5
|
| 12 |
def generate_questions(prompt):
|
| 13 |
input_text = f"Generate 5 survey questions on: {prompt}"
|
| 14 |
result = generator(input_text, max_length=150, num_return_sequences=1)[0]['generated_text']
|
| 15 |
-
|
| 16 |
-
return
|
| 17 |
|
| 18 |
-
# Start timer
|
| 19 |
def start_timer(qid):
|
| 20 |
paradata[qid] = {"start_time": time.time()}
|
| 21 |
-
return f"
|
| 22 |
|
| 23 |
-
# Submit response
|
| 24 |
def submit_response(answer, qid):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
# Gradio interface
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
-
gr.Markdown("## 🧠 AI Smart Survey
|
| 34 |
|
| 35 |
-
prompt = gr.Textbox(label="
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
for i, q in enumerate(questions):
|
| 47 |
-
q_box = gr.Textbox(value=q, label=f"Q{i+1}", interactive=False)
|
| 48 |
-
a_box = gr.Textbox(label=f"Answer Q{i+1}")
|
| 49 |
-
submit_btn = gr.Button(f"Submit Q{i+1}")
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
demo.launch()
|
|
|
|
| 2 |
import time
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Load question generation model
|
| 6 |
generator = pipeline("text-generation", model="gpt2")
|
| 7 |
|
| 8 |
+
# Track paradata
|
| 9 |
paradata = {}
|
| 10 |
|
| 11 |
+
# Generate 5 questions
|
| 12 |
def generate_questions(prompt):
|
| 13 |
input_text = f"Generate 5 survey questions on: {prompt}"
|
| 14 |
result = generator(input_text, max_length=150, num_return_sequences=1)[0]['generated_text']
|
| 15 |
+
lines = [line.strip() for line in result.split('\n') if line.strip()]
|
| 16 |
+
return lines[:5] + [""] * (5 - len(lines)) # Ensure 5 questions
|
| 17 |
|
| 18 |
+
# Start timer
|
| 19 |
def start_timer(qid):
|
| 20 |
paradata[qid] = {"start_time": time.time()}
|
| 21 |
+
return f"Started timer for Q{qid+1}"
|
| 22 |
|
| 23 |
+
# Submit response
|
| 24 |
def submit_response(answer, qid):
|
| 25 |
+
if qid in paradata and "start_time" in paradata[qid]:
|
| 26 |
+
duration = time.time() - paradata[qid]["start_time"]
|
| 27 |
+
paradata[qid]["response"] = answer
|
| 28 |
+
paradata[qid]["duration"] = duration
|
| 29 |
+
return f"Q{qid+1} submitted in {duration:.2f} seconds. Answer: {answer}"
|
| 30 |
+
else:
|
| 31 |
+
return f"Timer not started for Q{qid+1}. Please type your answer to begin."
|
| 32 |
|
|
|
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("## 🧠 AI Smart Survey Tool with Paradata Tracking")
|
| 35 |
|
| 36 |
+
prompt = gr.Textbox(label="Enter survey topic prompt")
|
| 37 |
+
generate = gr.Button("Generate Survey Questions")
|
| 38 |
|
| 39 |
+
qboxes = []
|
| 40 |
+
aboxes = []
|
| 41 |
+
submit_btns = []
|
| 42 |
+
logs = []
|
| 43 |
|
| 44 |
+
for i in range(5):
|
| 45 |
+
qbox = gr.Textbox(label=f"Q{i+1}", interactive=False)
|
| 46 |
+
abox = gr.Textbox(label=f"Answer Q{i+1}")
|
| 47 |
+
submit = gr.Button(f"Submit Q{i+1}")
|
| 48 |
+
log = gr.Textbox(label=f"Log Q{i+1}", interactive=False)
|
| 49 |
|
| 50 |
+
abox.change(start_timer, inputs=[gr.State(i)], outputs=log)
|
| 51 |
+
submit.click(submit_response, inputs=[abox, gr.State(i)], outputs=log)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
qboxes.append(qbox)
|
| 54 |
+
aboxes.append(abox)
|
| 55 |
+
submit_btns.append(submit)
|
| 56 |
+
logs.append(log)
|
| 57 |
|
| 58 |
+
# Fill in all questions when prompt is submitted
|
| 59 |
+
def fill_questions(prompt_text):
|
| 60 |
+
questions = generate_questions(prompt_text)
|
| 61 |
+
return questions
|
| 62 |
|
| 63 |
+
generate.click(fill_questions, inputs=prompt, outputs=qboxes)
|
| 64 |
|
| 65 |
demo.launch()
|