Spaces:
No application file
No application file
Nitish Shukla commited on
Commit ·
7902b91
1
Parent(s): 07c8bec
added app
Browse files- dataset-web.py +74 -0
dataset-web.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Paths
|
| 7 |
+
JSON_DIR = "/research/iprobe-shuklan3/face-DPO/conversations"
|
| 8 |
+
BASE_IMAGE_PATH = "/research/iprobe-shuklan3/face-DPO/Dataset/Train"
|
| 9 |
+
|
| 10 |
+
# Load all JSON files
|
| 11 |
+
def list_json_files():
|
| 12 |
+
return [f for f in os.listdir(JSON_DIR) if f.endswith(".json")]
|
| 13 |
+
|
| 14 |
+
# Load JSON content
|
| 15 |
+
def load_json(file_name):
|
| 16 |
+
with open(os.path.join(JSON_DIR, file_name), "r") as f:
|
| 17 |
+
return json.load(f)
|
| 18 |
+
|
| 19 |
+
# Select a random entry from JSON
|
| 20 |
+
def random_entry(json_data):
|
| 21 |
+
return random.choice(json_data["conversations"]) if "conversations" in json_data else None
|
| 22 |
+
|
| 23 |
+
# Gradio interface function
|
| 24 |
+
def show_random_entry(selected_json):
|
| 25 |
+
if not selected_json:
|
| 26 |
+
return "Select a JSON file first", [], "", ""
|
| 27 |
+
|
| 28 |
+
json_data = load_json(selected_json)
|
| 29 |
+
entry = random_entry(json_data)
|
| 30 |
+
if not entry:
|
| 31 |
+
return "No entries found in JSON.", [], "", ""
|
| 32 |
+
|
| 33 |
+
# Extract info
|
| 34 |
+
human_prompt = entry.get("conversations", [{}])[0].get("value", "")
|
| 35 |
+
chosen_text = entry.get("chosen", {}).get("value", "")
|
| 36 |
+
rejected_text = entry.get("rejected", {}).get("value", "")
|
| 37 |
+
|
| 38 |
+
# Build image paths
|
| 39 |
+
images = entry.get("images", [])
|
| 40 |
+
img_paths = []
|
| 41 |
+
for img in images:
|
| 42 |
+
# Make sure paths exist
|
| 43 |
+
abs_path = img
|
| 44 |
+
if os.path.exists(abs_path):
|
| 45 |
+
img_paths.append(abs_path)
|
| 46 |
+
else:
|
| 47 |
+
img_paths.append(None) # placeholder
|
| 48 |
+
|
| 49 |
+
return human_prompt, img_paths, chosen_text, rejected_text
|
| 50 |
+
|
| 51 |
+
# Build Gradio app
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
with gr.Row():
|
| 54 |
+
json_dropdown = gr.Dropdown(choices=list_json_files(), label="Select JSON")
|
| 55 |
+
randomize_btn = gr.Button("Randomize Entry")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
prompt_text = gr.Textbox(label="Prompt", interactive=False)
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
img_gallery = gr.Gallery(label="Images", elem_id="image_gallery").style(grid=[3], height="auto")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
chosen_textbox = gr.Textbox(label="GPT Chosen", interactive=False)
|
| 65 |
+
rejected_textbox = gr.Textbox(label="GPT Rejected", interactive=False)
|
| 66 |
+
|
| 67 |
+
# Connect button
|
| 68 |
+
randomize_btn.click(
|
| 69 |
+
fn=show_random_entry,
|
| 70 |
+
inputs=[json_dropdown],
|
| 71 |
+
outputs=[prompt_text, img_gallery, chosen_textbox, rejected_textbox]
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
demo.launch()
|