Jaswanth-0821 commited on
Commit
2fe91c7
·
verified ·
1 Parent(s): 3f3a9d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -28
app.py CHANGED
@@ -1,10 +1,9 @@
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
  import torch
4
-
5
  # ----------------------------- Load Models on Demand -----------------------------
6
  _loaded_models = {}
7
-
8
  def load_model(model_name):
9
  if model_name not in _loaded_models:
10
  _loaded_models[model_name] = SentenceTransformer(model_name)
@@ -30,7 +29,7 @@ def find_similar_documents(query, documents, model_name):
30
  sorted_indices = torch.argsort(similarities, descending=True)
31
  results = []
32
  for i, idx in enumerate(sorted_indices):
33
- score = similarities[0][idx].item()
34
  doc = doc_list[idx]
35
  results.append(f"{i+1}. Score: {score:.4f}\n Document: {doc}")
36
 
@@ -55,6 +54,7 @@ with gr.Blocks(
55
  with gr.Tabs():
56
  # ---------------- Single Model Tab ----------------
57
  with gr.Tab("🔹 Single Model Search"):
 
58
  with gr.Row():
59
  with gr.Column(scale=1):
60
  model_selector = gr.Dropdown(
@@ -77,10 +77,31 @@ with gr.Blocks(
77
 
78
  search_btn.click(find_similar_documents, [query_input, docs_input, model_selector], result_box)
79
  query_input.submit(find_similar_documents, [query_input, docs_input, model_selector], result_box)
80
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  # ---------------- Comparison Tab ----------------
82
  with gr.Tab("⚖️ Model Comparison"):
83
  with gr.Row():
 
84
  with gr.Column(scale=1):
85
  tarka_selector = gr.Dropdown(
86
  label="Tarka Model",
@@ -115,7 +136,27 @@ with gr.Blocks(
115
  with gr.Column():
116
  open_label = gr.Markdown(visible=False)
117
  open_output = gr.Markdown(visible=False)
118
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  def run_comparison(query, docs, tarka_model, open_model):
120
  tarka_res, open_res = compare_models(query, docs, tarka_model, open_model)
121
  return (
@@ -139,28 +180,9 @@ with gr.Blocks(
139
  )
140
 
141
  # ---------------- Example Section ----------------
142
- gr.Markdown("## 🧪 Try Examples")
143
-
144
- examples = [
145
- [
146
- "Which planet is known as the Red Planet?",
147
- "Venus is often called Earth's twin because of its similar size.\nMars, known for its reddish hue, is called the Red Planet.\nJupiter, the largest planet, has a red spot.\nSaturn has iconic rings."
148
- ],
149
- [
150
- "What causes seasons on Earth?",
151
- "The tilt of Earth's axis causes different sunlight distribution.\nThe moon affects tides but not seasons.\nEarth's orbit has minimal effect on seasons.\nRotation causes day and night."
152
- ],
153
- [
154
- "What gas do plants release during photosynthesis?",
155
- "Plants use sunlight to convert CO₂ into glucose and release oxygen.\nAnimals inhale oxygen and exhale CO₂.\nPhotosynthesis occurs mainly in leaves."
156
- ]
157
- ]
158
-
159
- gr.Examples(
160
- examples=examples,
161
- inputs=[query_input, docs_input],
162
- label="Try Example"
163
- )
164
 
165
  # Launch the app
166
- demo.launch()
 
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
  import torch
 
4
  # ----------------------------- Load Models on Demand -----------------------------
5
  _loaded_models = {}
6
+ tab_state = "single"
7
  def load_model(model_name):
8
  if model_name not in _loaded_models:
9
  _loaded_models[model_name] = SentenceTransformer(model_name)
 
29
  sorted_indices = torch.argsort(similarities, descending=True)
30
  results = []
31
  for i, idx in enumerate(sorted_indices):
32
+ score = similarities[idx].item()
33
  doc = doc_list[idx]
34
  results.append(f"{i+1}. Score: {score:.4f}\n Document: {doc}")
35
 
 
54
  with gr.Tabs():
55
  # ---------------- Single Model Tab ----------------
56
  with gr.Tab("🔹 Single Model Search"):
57
+ tab_state="single"
58
  with gr.Row():
59
  with gr.Column(scale=1):
60
  model_selector = gr.Dropdown(
 
77
 
78
  search_btn.click(find_similar_documents, [query_input, docs_input, model_selector], result_box)
79
  query_input.submit(find_similar_documents, [query_input, docs_input, model_selector], result_box)
80
+ examples = [
81
+ [
82
+ "Which planet is known as the Red Planet?",
83
+ "Venus is often called Earth's twin because of its similar size.\nMars, known for its reddish hue, is called the Red Planet.\nJupiter, the largest planet, has a red spot.\nSaturn has iconic rings."
84
+ ],
85
+ [
86
+ "What causes seasons on Earth?",
87
+ "The tilt of Earth's axis causes different sunlight distribution.\nThe moon affects tides but not seasons.\nEarth's orbit has minimal effect on seasons.\nRotation causes day and night."
88
+ ],
89
+ [
90
+ "What gas do plants release during photosynthesis?",
91
+ "Plants use sunlight to convert CO₂ into glucose and release oxygen.\nAnimals inhale oxygen and exhale CO₂.\nPhotosynthesis occurs mainly in leaves."
92
+ ]
93
+ ]
94
+
95
+
96
+ gr.Examples(
97
+ examples=examples,
98
+ inputs=[query_input, docs_input],
99
+ label="Try Examples"
100
+ )
101
  # ---------------- Comparison Tab ----------------
102
  with gr.Tab("⚖️ Model Comparison"):
103
  with gr.Row():
104
+ tab_state="comparsion"
105
  with gr.Column(scale=1):
106
  tarka_selector = gr.Dropdown(
107
  label="Tarka Model",
 
136
  with gr.Column():
137
  open_label = gr.Markdown(visible=False)
138
  open_output = gr.Markdown(visible=False)
139
+ examples = [
140
+ [
141
+ "Which planet is known as the Red Planet?",
142
+ "Venus is often called Earth's twin because of its similar size.\nMars, known for its reddish hue, is called the Red Planet.\nJupiter, the largest planet, has a red spot.\nSaturn has iconic rings."
143
+ ],
144
+ [
145
+ "What causes seasons on Earth?",
146
+ "The tilt of Earth's axis causes different sunlight distribution.\nThe moon affects tides but not seasons.\nEarth's orbit has minimal effect on seasons.\nRotation causes day and night."
147
+ ],
148
+ [
149
+ "What gas do plants release during photosynthesis?",
150
+ "Plants use sunlight to convert CO₂ into glucose and release oxygen.\nAnimals inhale oxygen and exhale CO₂.\nPhotosynthesis occurs mainly in leaves."
151
+ ]
152
+ ]
153
+
154
+
155
+ gr.Examples(
156
+ examples=examples,
157
+ inputs=[cmp_query, cmp_docs],
158
+ label="Try Examples"
159
+ )
160
  def run_comparison(query, docs, tarka_model, open_model):
161
  tarka_res, open_res = compare_models(query, docs, tarka_model, open_model)
162
  return (
 
180
  )
181
 
182
  # ---------------- Example Section ----------------
183
+
184
+
185
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
  # Launch the app
188
+ demo.launch()