malusama commited on
Commit
0ab025b
·
verified ·
1 Parent(s): 8cec082

Simplify Gradio schema and disable API docs

Browse files
Files changed (1) hide show
  1. app.py +16 -18
app.py CHANGED
@@ -63,21 +63,22 @@ def run_demo(image: Image.Image, candidate_text: str):
63
  scores = (image_outputs.image_embeds @ text_outputs.text_embeds.t()).squeeze(0)
64
  probs = scores.softmax(dim=-1)
65
 
66
- table = [
67
- [label, float(score), float(prob)]
68
  for label, score, prob in zip(labels, scores.tolist(), probs.tolist())
69
  ]
70
- table.sort(key=lambda row: row[2], reverse=True)
71
 
72
- top_label = table[0][0]
73
- top_prob = table[0][2]
74
  summary = f"Top match: {top_label} ({top_prob:.4f})"
75
- raw = {
76
- "labels": labels,
77
- "scores": scores.tolist(),
78
- "probs": probs.tolist(),
 
79
  }
80
- return summary, table, json.dumps(raw, ensure_ascii=False, indent=2)
81
 
82
 
83
  def build_demo():
@@ -103,17 +104,14 @@ def build_demo():
103
 
104
  run_button = gr.Button("Run Matching", variant="primary")
105
  summary_output = gr.Textbox(label="Summary")
106
- table_output = gr.Dataframe(
107
- headers=["label", "score", "prob"],
108
- datatype=["str", "number", "number"],
109
- label="Results",
110
- )
111
- json_output = gr.Code(label="Raw Output", language="json")
112
 
113
  run_button.click(
114
  run_demo,
115
  inputs=[image_input, labels_input],
116
- outputs=[summary_output, table_output, json_output],
 
 
117
  )
118
  return demo
119
 
@@ -126,4 +124,4 @@ except ModuleNotFoundError:
126
  if __name__ == "__main__":
127
  if demo is None:
128
  raise RuntimeError("gradio is required to launch this app.")
129
- demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
 
63
  scores = (image_outputs.image_embeds @ text_outputs.text_embeds.t()).squeeze(0)
64
  probs = scores.softmax(dim=-1)
65
 
66
+ rows = [
67
+ (label, float(score), float(prob))
68
  for label, score, prob in zip(labels, scores.tolist(), probs.tolist())
69
  ]
70
+ rows.sort(key=lambda row: row[2], reverse=True)
71
 
72
+ top_label = rows[0][0]
73
+ top_prob = rows[0][2]
74
  summary = f"Top match: {top_label} ({top_prob:.4f})"
75
+ details = {
76
+ "ranked_results": [
77
+ {"label": label, "score": score, "prob": prob}
78
+ for label, score, prob in rows
79
+ ]
80
  }
81
+ return summary, json.dumps(details, ensure_ascii=False, indent=2)
82
 
83
 
84
  def build_demo():
 
104
 
105
  run_button = gr.Button("Run Matching", variant="primary")
106
  summary_output = gr.Textbox(label="Summary")
107
+ details_output = gr.Textbox(label="Results JSON", lines=18)
 
 
 
 
 
108
 
109
  run_button.click(
110
  run_demo,
111
  inputs=[image_input, labels_input],
112
+ outputs=[summary_output, details_output],
113
+ api_name=False,
114
+ show_api=False,
115
  )
116
  return demo
117
 
 
124
  if __name__ == "__main__":
125
  if demo is None:
126
  raise RuntimeError("gradio is required to launch this app.")
127
+ demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)