eli02 commited on
Commit
bce0331
Β·
1 Parent(s): 851a0e2

update: Add frontend build script, enhance .gitignore, and update dependencies in requirements.txt

Browse files
.gitignore CHANGED
@@ -1,2 +1,5 @@
1
  .venv
2
- check_dataset.ipynb
 
 
 
 
1
  .venv
2
+ node_modules/
3
+ __pycache__/
4
+ *.pyc
5
+ .env
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Rag Retrieval
3
  emoji: πŸ†
4
  colorFrom: gray
5
  colorTo: red
@@ -8,6 +8,9 @@ sdk_version: 1.41.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
 
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: EnlightenQalb
3
  emoji: πŸ†
4
  colorFrom: gray
5
  colorTo: red
 
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
+ build:
12
+ - pip install -r requirements.txt
13
+ - bash build.sh
14
  ---
15
 
16
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -8,6 +8,15 @@ import pandas as pd
8
  import numpy as np
9
  import torch as t
10
  import os
 
 
 
 
 
 
 
 
 
11
 
12
  # Cache the model loading
13
  @st.cache_resource
@@ -46,8 +55,7 @@ def debug_check_before_save(data_dict):
46
 
47
  return len(set(lengths.values())) == 1 # Returns True if all lengths match
48
 
49
- def save_reactions_to_dataset(user_type, username, query, results_mpnet, results_openai):
50
- # First prepare the new data
51
  data = {
52
  "user_type": [],
53
  "username": [],
@@ -56,23 +64,14 @@ def save_reactions_to_dataset(user_type, username, query, results_mpnet, results
56
  "model_type": [],
57
  "reaction": []
58
  }
59
-
60
- # Add results from MPNet
61
- for result in results_mpnet:
62
- data["user_type"].append(user_type)
63
- data["username"].append(username)
64
- data["query"].append(query)
65
- data["retrieved_text"].append(result["text"])
66
- data["model_type"].append("all-mpnet-base-v2")
67
- data["reaction"].append(result["reaction"])
68
-
69
- # Add results from OpenAI
70
- for result in results_openai:
71
  data["user_type"].append(user_type)
72
  data["username"].append(username)
73
  data["query"].append(query)
74
  data["retrieved_text"].append(result["text"])
75
- data["model_type"].append("openai")
76
  data["reaction"].append(result["reaction"])
77
 
78
  try:
@@ -228,80 +227,70 @@ def main():
228
  st.write(f"Time taken to compute scores: {end_time - start_time:.5f} seconds")
229
  st.session_state.search_performed = True
230
 
231
- # Display results and collect reactions
232
  if st.session_state.search_performed and not st.session_state.results_saved:
233
- st.subheader("Query Results")
234
- st.write(f"Query: {query}")
235
-
236
- # Display MPNet results
237
- st.markdown("### Results from MPNet Model")
238
- for idx in st.session_state.top_results_mpnet:
239
- text = df.iloc[int(idx)]["ext"]
240
- st.write(f"**Text:** {text}")
241
-
242
- key = f"reaction_mpnet_{idx}"
243
- if key not in st.session_state.reactions:
244
- st.session_state.reactions[key] = "🀷"
245
-
246
- reaction = st.radio(
247
- label=f"Rate this MPNet result (Result {idx}):",
248
- options=["πŸ‘Ž", "🀷", "πŸ‘"],
249
- index=["πŸ‘Ž", "🀷", "πŸ‘"].index(st.session_state.reactions[key]),
250
- key=key,
251
- horizontal=True,
252
- on_change=update_reaction,
253
- args=("mpnet", idx)
 
 
 
 
 
 
 
 
254
  )
255
-
256
- # Display OpenAI results
257
- st.markdown("### Results from OpenAI Model")
258
- for idx in st.session_state.top_results_openai:
259
- text = df.iloc[int(idx)]["ext"]
260
- st.write(f"**Text:** {text}")
261
-
262
- key = f"reaction_openai_{idx}"
263
- if key not in st.session_state.reactions:
264
- st.session_state.reactions[key] = "🀷"
265
-
266
- reaction = st.radio(
267
- label=f"Rate this OpenAI result (Result {idx}):",
268
- options=["πŸ‘Ž", "🀷", "πŸ‘"],
269
- index=["πŸ‘Ž", "🀷", "πŸ‘"].index(st.session_state.reactions[key]),
270
- key=key,
271
- horizontal=True,
272
- on_change=update_reaction,
273
- args=("openai", idx)
274
  )
275
 
276
- # Save reactions button
277
- if st.button("Save Reactions"):
278
- # Collect MPNet results
279
- results_mpnet = []
280
- for idx in st.session_state.top_results_mpnet:
281
- key = f"reaction_mpnet_{idx}"
282
- results_mpnet.append({
283
- "text": df.iloc[int(idx)]["ext"],
284
- "reaction": st.session_state.reactions[key]
285
- })
286
-
287
- # Collect OpenAI results
288
- results_openai = []
289
- for idx in st.session_state.top_results_openai:
290
- key = f"reaction_openai_{idx}"
291
- results_openai.append({
292
- "text": df.iloc[int(idx)]["ext"],
293
- "reaction": st.session_state.reactions[key]
294
- })
295
-
296
- save_reactions_to_dataset(
297
- user_type,
298
- st.session_state.username,
299
- query,
300
- results_mpnet,
301
- results_openai
302
- )
303
- st.success("Reactions saved successfully!")
304
- clear_search_state()
305
 
306
  except Exception as e:
307
  st.error(f"Failed to load database: {str(e)}")
 
8
  import numpy as np
9
  import torch as t
10
  import os
11
+ import random
12
+ import streamlit.components.v1 as components
13
+
14
+ # Define the React component
15
+ def search_results_component(query, results, key=None):
16
+ return components.declare_component(
17
+ "search_results",
18
+ path="frontend/build" # Path to your built React component
19
+ )(query=query, results=results, key=key)
20
 
21
  # Cache the model loading
22
  @st.cache_resource
 
55
 
56
  return len(set(lengths.values())) == 1 # Returns True if all lengths match
57
 
58
+ def save_reactions_to_dataset(user_type, username, query, results):
 
59
  data = {
60
  "user_type": [],
61
  "username": [],
 
64
  "model_type": [],
65
  "reaction": []
66
  }
67
+
68
+ # Add results
69
+ for result in results:
 
 
 
 
 
 
 
 
 
70
  data["user_type"].append(user_type)
71
  data["username"].append(username)
72
  data["query"].append(query)
73
  data["retrieved_text"].append(result["text"])
74
+ data["model_type"].append(result["model"])
75
  data["reaction"].append(result["reaction"])
76
 
77
  try:
 
227
  st.write(f"Time taken to compute scores: {end_time - start_time:.5f} seconds")
228
  st.session_state.search_performed = True
229
 
 
230
  if st.session_state.search_performed and not st.session_state.results_saved:
231
+ st.subheader("Compare Results")
232
+
233
+ # Prepare results in the required format
234
+ results = [
235
+ {
236
+ "model": "all-mpnet-base-v2",
237
+ "text": df.iloc[int(st.session_state.top_results_mpnet[0])]["ext"]
238
+ },
239
+ {
240
+ "model": "openai",
241
+ "text": df.iloc[int(st.session_state.top_results_openai[0])]["ext"]
242
+ }
243
+ ]
244
+
245
+ # Use Streamlit's columns for side-by-side display
246
+ col1, col2 = st.columns(2)
247
+
248
+ # Randomly decide which result goes in which column
249
+ if random.random() < 0.5:
250
+ results = results[::-1]
251
+
252
+ with col1:
253
+ st.markdown("### Result A")
254
+ st.write(results[0]["text"])
255
+ reaction_a = st.radio(
256
+ "Rate Result A:",
257
+ ["πŸ‘Ž", "🀷", "πŸ‘"],
258
+ key=f"reaction_a",
259
+ horizontal=True
260
  )
261
+
262
+ with col2:
263
+ st.markdown("### Result B")
264
+ st.write(results[1]["text"])
265
+ reaction_b = st.radio(
266
+ "Rate Result B:",
267
+ ["πŸ‘Ž", "🀷", "πŸ‘"],
268
+ key=f"reaction_b",
269
+ horizontal=True
 
 
 
 
 
 
 
 
 
 
270
  )
271
 
272
+ if st.button("Save Reactions"):
273
+ results_to_save = [
274
+ {
275
+ "model": results[0]["model"],
276
+ "text": results[0]["text"],
277
+ "reaction": reaction_a
278
+ },
279
+ {
280
+ "model": results[1]["model"],
281
+ "text": results[1]["text"],
282
+ "reaction": reaction_b
283
+ }
284
+ ]
285
+
286
+ save_reactions_to_dataset(
287
+ user_type,
288
+ st.session_state.username,
289
+ query,
290
+ results_to_save
291
+ )
292
+ st.success("Reactions saved successfully!")
293
+ clear_search_state()
 
 
 
 
 
 
 
294
 
295
  except Exception as e:
296
  st.error(f"Failed to load database: {str(e)}")
build.sh ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ cd frontend
3
+ npm install
4
+ npm run build
5
+ cd ..
frontend/components/SearchResults.jsx ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState, useEffect } from 'react';
2
+ import { Card, CardContent } from "@/components/ui/card";
3
+ import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
4
+ import { Label } from "@/components/ui/label";
5
+ import { Button } from "@/components/ui/button";
6
+ import { ArrowLeftRight } from "lucide-react";
7
+
8
+ const SearchResults = ({ query, results, onSaveReactions }) => {
9
+ const [randomizedResults, setRandomizedResults] = useState([]);
10
+ const [reactions, setReactions] = useState({});
11
+
12
+ // Randomize the order of results on initial load
13
+ useEffect(() => {
14
+ const modelA = results[0];
15
+ const modelB = results[1];
16
+
17
+ // Randomly decide which model goes left/right
18
+ const isModelALeft = Math.random() < 0.5;
19
+ setRandomizedResults([
20
+ {
21
+ id: isModelALeft ? 'left' : 'right',
22
+ text: modelA.text,
23
+ originalModel: modelA.model
24
+ },
25
+ {
26
+ id: isModelALeft ? 'right' : 'left',
27
+ text: modelB.text,
28
+ originalModel: modelB.model
29
+ }
30
+ ]);
31
+ }, [results]);
32
+
33
+ const handleReactionChange = (position, value) => {
34
+ setReactions(prev => ({
35
+ ...prev,
36
+ [position]: value
37
+ }));
38
+ };
39
+
40
+ const handleSave = () => {
41
+ // Map reactions back to their original models
42
+ const mappedReactions = randomizedResults.map(result => ({
43
+ model: result.originalModel,
44
+ text: result.text,
45
+ reaction: reactions[result.id] || '🀷'
46
+ }));
47
+ onSaveReactions(mappedReactions);
48
+ };
49
+
50
+ return (
51
+ <div className="w-full space-y-6">
52
+ <div className="text-lg font-medium">Query: {query}</div>
53
+
54
+ <div className="grid grid-cols-2 gap-4">
55
+ {randomizedResults.map((result) => (
56
+ <Card key={result.id} className="p-4">
57
+ <CardContent>
58
+ <div className="space-y-4">
59
+ <div className="text-sm font-medium">Result {result.id === 'left' ? 'A' : 'B'}</div>
60
+ <div className="min-h-32 p-4 bg-gray-50 rounded-lg">
61
+ {result.text}
62
+ </div>
63
+
64
+ <RadioGroup
65
+ defaultValue="🀷"
66
+ onValueChange={(value) => handleReactionChange(result.id, value)}
67
+ className="flex justify-center space-x-4"
68
+ >
69
+ <div className="flex items-center space-x-2">
70
+ <RadioGroupItem value="πŸ‘Ž" id={`thumbsdown-${result.id}`} />
71
+ <Label htmlFor={`thumbsdown-${result.id}`}>πŸ‘Ž</Label>
72
+ </div>
73
+ <div className="flex items-center space-x-2">
74
+ <RadioGroupItem value="🀷" id={`neutral-${result.id}`} />
75
+ <Label htmlFor={`neutral-${result.id}`}>🀷</Label>
76
+ </div>
77
+ <div className="flex items-center space-x-2">
78
+ <RadioGroupItem value="πŸ‘" id={`thumbsup-${result.id}`} />
79
+ <Label htmlFor={`thumbsup-${result.id}`}>πŸ‘</Label>
80
+ </div>
81
+ </RadioGroup>
82
+ </div>
83
+ </CardContent>
84
+ </Card>
85
+ ))}
86
+ </div>
87
+
88
+ <Button
89
+ onClick={handleSave}
90
+ className="w-full"
91
+ >
92
+ Save Reactions
93
+ </Button>
94
+ </div>
95
+ );
96
+ };
97
+
98
+ export default SearchResults;
frontend/index.html ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>EnlightenQalb Results</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ </head>
9
+ <body>
10
+ <div id="root"></div>
11
+ <script type="module" src="./index.jsx"></script>
12
+ </body>
13
+ </html>
frontend/index.jsx ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import React from 'react';
2
+ import ReactDOM from 'react-dom/client';
3
+ import SearchResults from './components/SearchResults';
4
+
5
+ ReactDOM.createRoot(document.getElementById('root')).render(
6
+ <React.StrictMode>
7
+ <SearchResults />
8
+ </React.StrictMode>
9
+ );
package.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "enlightenqalb",
3
+ "version": "1.0.0",
4
+ "dependencies": {
5
+ "react": "^18.2.0",
6
+ "react-dom": "^18.2.0",
7
+ "@radix-ui/react-radio-group": "^1.1.3",
8
+ "@radix-ui/react-label": "^2.0.2",
9
+ "lucide-react": "^0.263.1",
10
+ "class-variance-authority": "^0.7.0",
11
+ "clsx": "^2.0.0",
12
+ "tailwind-merge": "^1.14.0"
13
+ }
14
+ }
requirements.txt CHANGED
@@ -1,4 +1,8 @@
1
- torch
2
- pandas
3
  sentence-transformers
4
- openai
 
 
 
 
 
1
+ streamlit>=1.41.1
2
+ openai
3
  sentence-transformers
4
+ huggingface-hub
5
+ datasets
6
+ pandas
7
+ numpy
8
+ torch