pawanmakes commited on
Commit
f56945a
·
1 Parent(s): 912053a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -1,15 +1,19 @@
1
  import gradio as gr
 
 
2
 
3
  # --- 1. Your AI Integration Here ---
 
4
  def generate_mcqs(image):
5
- # Replace this with your actual model inference
6
- # Return a structured list of dictionaries
 
 
7
  mock_mcqs = [
8
  {"q": "What is the main object in this image?", "options": ["Car", "Dog", "Tree", "House"], "a": "Dog"},
9
  {"q": "What color is the sky?", "options": ["Blue", "Red", "Green", "Yellow"], "a": "Blue"}
10
  ]
11
 
12
- # Return: mcq_state, current_index, score, upload_row_update, quiz_row_update, question_text, radio_options
13
  return (
14
  mock_mcqs,
15
  0,
@@ -22,7 +26,6 @@ def generate_mcqs(image):
22
 
23
  # --- 2. State Management for the Quiz ---
24
  def handle_answer(selected_option, mcqs, current_idx, score):
25
- # Check if correct
26
  correct_answer = mcqs[current_idx]["a"]
27
  if selected_option == correct_answer:
28
  score += 1
@@ -35,9 +38,10 @@ def handle_answer(selected_option, mcqs, current_idx, score):
35
  return (
36
  current_idx,
37
  score,
38
- gr.update(visible=False), # Hide quiz
39
- gr.update(visible=True, value=final_msg), # Show results
40
- "",
 
41
  gr.update()
42
  )
43
 
@@ -48,15 +52,15 @@ def handle_answer(selected_option, mcqs, current_idx, score):
48
  return (
49
  current_idx,
50
  score,
51
- gr.update(visible=True),
52
- gr.update(visible=False),
53
- next_q,
54
- gr.update(choices=next_options, value=None)
 
55
  )
56
 
57
  # --- 3. The UI Layout ---
58
  with gr.Blocks() as demo:
59
- # Hidden state variables
60
  state_mcqs = gr.State([])
61
  state_idx = gr.State(0)
62
  state_score = gr.State(0)
@@ -76,7 +80,7 @@ with gr.Blocks() as demo:
76
 
77
  # Phase 3: Results Layout
78
  with gr.Row(visible=False) as results_row:
79
- results_display = gr.Markdown("### Results")
80
 
81
  # --- 4. Event Wiring ---
82
  btn_generate.click(
@@ -88,7 +92,8 @@ with gr.Blocks() as demo:
88
  btn_next.click(
89
  fn=handle_answer,
90
  inputs=[options_radio, state_mcqs, state_idx, state_score],
91
- outputs=[state_idx, state_score, quiz_row, results_row, question_display, options_radio]
 
92
  )
93
 
94
  demo.launch()
 
1
  import gradio as gr
2
+ import spaces # <-- 1. Import spaces
3
+ import torch
4
 
5
  # --- 1. Your AI Integration Here ---
6
+ @spaces.GPU # <-- 2. Apply this to the function doing the heavy lifting
7
  def generate_mcqs(image):
8
+ # Your actual model inference goes here.
9
+ # Because of the decorator, any tensors you move to '.cuda()' in here will work.
10
+
11
+ # Mock data for demonstration
12
  mock_mcqs = [
13
  {"q": "What is the main object in this image?", "options": ["Car", "Dog", "Tree", "House"], "a": "Dog"},
14
  {"q": "What color is the sky?", "options": ["Blue", "Red", "Green", "Yellow"], "a": "Blue"}
15
  ]
16
 
 
17
  return (
18
  mock_mcqs,
19
  0,
 
26
 
27
  # --- 2. State Management for the Quiz ---
28
  def handle_answer(selected_option, mcqs, current_idx, score):
 
29
  correct_answer = mcqs[current_idx]["a"]
30
  if selected_option == correct_answer:
31
  score += 1
 
38
  return (
39
  current_idx,
40
  score,
41
+ gr.update(visible=False), # Hide quiz_row
42
+ gr.update(visible=True), # Show results_row
43
+ gr.update(value=final_msg), # <-- 3. Update the text of results_display
44
+ gr.update(),
45
  gr.update()
46
  )
47
 
 
52
  return (
53
  current_idx,
54
  score,
55
+ gr.update(visible=True), # Keep quiz_row visible
56
+ gr.update(visible=False), # Keep results_row hidden
57
+ gr.update(), # Don't change results_display yet
58
+ gr.update(value=next_q), # Update question_display
59
+ gr.update(choices=next_options, value=None) # Update options_radio
60
  )
61
 
62
  # --- 3. The UI Layout ---
63
  with gr.Blocks() as demo:
 
64
  state_mcqs = gr.State([])
65
  state_idx = gr.State(0)
66
  state_score = gr.State(0)
 
80
 
81
  # Phase 3: Results Layout
82
  with gr.Row(visible=False) as results_row:
83
+ results_display = gr.Markdown("### Results") # <-- 4. This is what we target with the final text
84
 
85
  # --- 4. Event Wiring ---
86
  btn_generate.click(
 
92
  btn_next.click(
93
  fn=handle_answer,
94
  inputs=[options_radio, state_mcqs, state_idx, state_score],
95
+ # Notice we added results_display to the outputs here to receive the final message
96
+ outputs=[state_idx, state_score, quiz_row, results_row, results_display, question_display, options_radio]
97
  )
98
 
99
  demo.launch()