Spaces:
Running
Running
update: Add frontend build script, enhance .gitignore, and update dependencies in requirements.txt
Browse files- .gitignore +4 -1
- README.md +4 -1
- app.py +74 -85
- build.sh +5 -0
- frontend/components/SearchResults.jsx +98 -0
- frontend/index.html +13 -0
- frontend/index.jsx +9 -0
- package.json +14 -0
- requirements.txt +7 -3
.gitignore
CHANGED
@@ -1,2 +1,5 @@
|
|
1 |
.venv
|
2 |
-
|
|
|
|
|
|
|
|
1 |
.venv
|
2 |
+
node_modules/
|
3 |
+
__pycache__/
|
4 |
+
*.pyc
|
5 |
+
.env
|
README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
---
|
2 |
-
title:
|
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,
|
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
|
61 |
-
for result in
|
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("
|
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("
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
)
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
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 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
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 |
-
|
2 |
-
|
3 |
sentence-transformers
|
4 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit>=1.41.1
|
2 |
+
openai
|
3 |
sentence-transformers
|
4 |
+
huggingface-hub
|
5 |
+
datasets
|
6 |
+
pandas
|
7 |
+
numpy
|
8 |
+
torch
|