Update app.py
Browse files
app.py
CHANGED
|
@@ -1,218 +1,225 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import json
|
| 7 |
-
import
|
| 8 |
-
from datasets import Dataset
|
| 9 |
-
from huggingface_hub import HfApi, login
|
| 10 |
import time
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
|
|
|
| 13 |
from gradio_modal import Modal
|
| 14 |
-
import
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
checkpoint = "marin-community/marin-8b-instruct"
|
| 19 |
-
device = "cuda"
|
|
|
|
|
|
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 21 |
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
DATASET_REPO = "WillHeld/model-feedback" #
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
| 39 |
-
|
| 40 |
-
# Prepare the feedback data
|
| 41 |
-
feedback_data = {
|
| 42 |
-
"id": feedback_id,
|
| 43 |
-
"timestamp": timestamp,
|
| 44 |
"conversation": conversation,
|
| 45 |
"satisfaction": satisfaction,
|
| 46 |
-
"feedback": feedback_text
|
| 47 |
}
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
try:
|
| 67 |
-
|
| 68 |
-
login(token=hf_token)
|
| 69 |
-
|
| 70 |
-
# Check if we have data to push
|
| 71 |
-
feedback_file = os.path.join(DATASET_PATH, DATASET_FILENAME)
|
| 72 |
-
if not os.path.exists(feedback_file):
|
| 73 |
-
print("No feedback data to push.")
|
| 74 |
-
return False
|
| 75 |
-
|
| 76 |
-
# Load data from the JSONL file
|
| 77 |
-
with open(feedback_file, "r") as f:
|
| 78 |
-
feedback_data = [json.loads(line) for line in f]
|
| 79 |
-
|
| 80 |
-
# Create a dataset from the feedback data
|
| 81 |
-
dataset = Dataset.from_list(feedback_data)
|
| 82 |
-
|
| 83 |
-
# Push to Hub
|
| 84 |
-
dataset.push_to_hub(
|
| 85 |
DATASET_REPO,
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
)
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
except Exception
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
# Modified predict function to update conversation state
|
| 97 |
@spaces.GPU(duration=120)
|
| 98 |
-
def
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
history.append({"role": "user", "content": message})
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True,
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
"""
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
status_msg = "Thank you for your valuable feedback! Your insights have been saved to the dataset."
|
| 148 |
-
else:
|
| 149 |
-
status_msg = "Thank you for your feedback! It has been saved locally, but couldn't be pushed to the dataset. Please check server logs."
|
| 150 |
-
|
| 151 |
-
return status_msg
|
| 152 |
-
|
| 153 |
-
# Create the Gradio blocks interface
|
| 154 |
-
with gr.Blocks() as demo:
|
| 155 |
-
# State to track conversation history
|
| 156 |
conversation_state = gr.State([])
|
| 157 |
-
|
| 158 |
with gr.Row():
|
|
|
|
| 159 |
with gr.Column(scale=3):
|
| 160 |
-
# Custom chat function wrapper to update state
|
| 161 |
-
def chat_with_state(message, history, state, temperature, top_p):
|
| 162 |
-
for partial_response, updated_state in predict(message, history, state, temperature, top_p):
|
| 163 |
-
# Update our state with each yield
|
| 164 |
-
state = updated_state
|
| 165 |
-
yield partial_response, state
|
| 166 |
-
|
| 167 |
-
# Create ChatInterface
|
| 168 |
chatbot = gr.ChatInterface(
|
| 169 |
-
|
| 170 |
-
additional_inputs=[
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
additional_outputs=[conversation_state],
|
| 176 |
-
type="messages"
|
| 177 |
)
|
| 178 |
-
|
|
|
|
| 179 |
with gr.Column(scale=1):
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
#
|
| 183 |
-
with Modal(visible=False) as
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
lambda: Modal(visible=True),
|
| 206 |
-
None,
|
| 207 |
-
feedback_modal
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
# Connect the submit button to the submit_research_feedback function with the current conversation state
|
| 211 |
-
submit_button.click(
|
| 212 |
-
submit_research_feedback,
|
| 213 |
-
inputs=[conversation_state, satisfaction, feedback_text],
|
| 214 |
-
outputs=response_text
|
| 215 |
)
|
| 216 |
|
| 217 |
-
#
|
| 218 |
-
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""HFΒ Space for the *Marinβ8BβInstruct* research preview
|
| 3 |
+
-----------------------------------------------------
|
| 4 |
+
A lightweight Gradio interface that
|
| 5 |
+
β’ streams chat completions from the `marin-community/marin-8b-instruct` model
|
| 6 |
+
β’ lets testers submit structured feedback (UX ratingΒ + freeβtext)
|
| 7 |
+
β’ appends feedback to a local JSONL *and* merges it into a private Hub dataset
|
| 8 |
+
The dataset is never overwritten: we always pull, merge, deduplicate, and push.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
# ββ standard lib
|
| 14 |
import json
|
| 15 |
+
import os
|
|
|
|
|
|
|
| 16 |
import time
|
| 17 |
+
import uuid
|
| 18 |
+
from threading import Thread
|
| 19 |
|
| 20 |
+
# ββ thirdβparty deps (declared in requirements.txt of the Space)
|
| 21 |
+
import gradio as gr
|
| 22 |
from gradio_modal import Modal
|
| 23 |
+
from transformers import (
|
| 24 |
+
AutoModelForCausalLM,
|
| 25 |
+
AutoTokenizer,
|
| 26 |
+
TextIteratorStreamer,
|
| 27 |
+
)
|
| 28 |
+
from datasets import Dataset, load_dataset, concatenate_datasets, DownloadMode
|
| 29 |
+
from huggingface_hub import HfApi, login
|
| 30 |
+
import spaces
|
| 31 |
|
| 32 |
+
# ββββββββββββββββββββββββββββ modelΒ & constants βββββββββββββββββββββββββββββ
|
| 33 |
checkpoint = "marin-community/marin-8b-instruct"
|
| 34 |
+
device = "cuda" # the Space runner gives us a GPU
|
| 35 |
+
|
| 36 |
+
# download π₯
|
| 37 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 38 |
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
|
| 39 |
|
| 40 |
+
# feedbackΒ dataset details
|
| 41 |
+
DATASET_REPO = "WillHeld/model-feedback" # <ββ change to your namespace if needed
|
| 42 |
+
DATA_DIR = "./feedback_data"
|
| 43 |
+
DATA_FILE = "feedback.jsonl"
|
| 44 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 45 |
+
|
| 46 |
+
# ββββββββββββββββββββββββββββ helpers βββββββββββββββββββββββββββββββββββββββ
|
| 47 |
+
|
| 48 |
+
def save_feedback_locally(conversation: list[dict[str, str]],
|
| 49 |
+
satisfaction: str,
|
| 50 |
+
feedback_text: str) -> str:
|
| 51 |
+
"""Append a single feedback record to a JSONL file and return its UUID."""
|
| 52 |
+
record = {
|
| 53 |
+
"id": str(uuid.uuid4()),
|
| 54 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
"conversation": conversation,
|
| 56 |
"satisfaction": satisfaction,
|
| 57 |
+
"feedback": feedback_text,
|
| 58 |
}
|
| 59 |
+
fp = os.path.join(DATA_DIR, DATA_FILE)
|
| 60 |
+
with open(fp, "a", encoding="utfβ8") as f:
|
| 61 |
+
f.write(json.dumps(record, ensure_ascii=False) + "\n")
|
| 62 |
+
return record["id"]
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def push_feedback_to_hub(hf_token: str | None = None) -> bool: # noqa: C901
|
| 66 |
+
"""Merge freshly collected feedback with whatβs already on the Hub.
|
| 67 |
+
|
| 68 |
+
Steps
|
| 69 |
+
-----
|
| 70 |
+
1. Authenticate with `hf_token` (fall back to $HF_TOKEN env).
|
| 71 |
+
2. Load *local* feedback just written in `feedback.jsonl`.
|
| 72 |
+
3. Pull existing remote split (if any); concat & `unique("id")`.
|
| 73 |
+
4. Push the merged dataset back.Β Never deletes remote shards β safe.
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
hf_token = hf_token or os.getenv("HF_TOKEN")
|
| 77 |
+
if not hf_token:
|
| 78 |
+
print("β No HF token β skipping Hub push.")
|
| 79 |
+
return False
|
| 80 |
+
login(token=hf_token)
|
| 81 |
+
|
| 82 |
+
fp = os.path.join(DATA_DIR, DATA_FILE)
|
| 83 |
+
if not os.path.exists(fp):
|
| 84 |
+
print("β Local feedback file missing; nothing to push.")
|
| 85 |
+
return False
|
| 86 |
+
|
| 87 |
+
# local rows β Dataset
|
| 88 |
+
with open(fp, encoding="utfβ8") as f:
|
| 89 |
+
local_ds = Dataset.from_list([json.loads(l) for l in f])
|
| 90 |
+
|
| 91 |
+
# try to pull remote
|
| 92 |
try:
|
| 93 |
+
remote_ds = load_dataset(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
DATASET_REPO,
|
| 95 |
+
split="train",
|
| 96 |
+
token=hf_token,
|
| 97 |
+
download_mode=DownloadMode.FORCE_REDOWNLOAD,
|
| 98 |
)
|
| 99 |
+
merged = concatenate_datasets([remote_ds, local_ds]).unique("id")
|
| 100 |
+
except FileNotFoundError:
|
| 101 |
+
# repo exists but empty
|
| 102 |
+
merged = local_ds
|
| 103 |
+
except Exception:
|
| 104 |
+
# repo may not exist yet β create & start fresh
|
| 105 |
+
HfApi(token=hf_token).create_repo(
|
| 106 |
+
repo_id=DATASET_REPO, repo_type="dataset", private=True
|
| 107 |
+
)
|
| 108 |
+
merged = local_ds
|
| 109 |
+
|
| 110 |
+
merged.push_to_hub(
|
| 111 |
+
DATASET_REPO,
|
| 112 |
+
private=True,
|
| 113 |
+
commit_message=f"Add {len(local_ds)} new feedback entries",
|
| 114 |
+
)
|
| 115 |
+
print(
|
| 116 |
+
f"β
Pushed {len(local_ds)} rows; dataset now has {len(merged)} total.")
|
| 117 |
+
# (optional) clear local file once synced
|
| 118 |
+
# os.remove(fp)
|
| 119 |
+
return True
|
| 120 |
+
|
| 121 |
+
# ββββββββββββββββββββββββββββ chat backend βββββββββββββββββββββββββββββββββ
|
| 122 |
|
|
|
|
| 123 |
@spaces.GPU(duration=120)
|
| 124 |
+
def generate_response(message: str,
|
| 125 |
+
history: list[dict[str, str]],
|
| 126 |
+
temperature: float,
|
| 127 |
+
top_p: float):
|
| 128 |
+
"""Streaming generator used by the Gradio ChatInterface."""
|
| 129 |
+
|
| 130 |
+
# 1) add user message to history
|
| 131 |
history.append({"role": "user", "content": message})
|
| 132 |
+
|
| 133 |
+
# 2) build model input via chat template
|
| 134 |
+
prompt = tokenizer.apply_chat_template(history, tokenize=False,
|
| 135 |
+
add_generation_prompt=True)
|
| 136 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
|
| 137 |
+
|
| 138 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True,
|
| 139 |
+
skip_special_tokens=True)
|
| 140 |
+
|
| 141 |
+
gen_kwargs = dict(
|
| 142 |
+
input_ids=input_ids,
|
| 143 |
+
max_new_tokens=1024,
|
| 144 |
+
temperature=float(temperature),
|
| 145 |
+
top_p=float(top_p),
|
| 146 |
+
do_sample=True,
|
| 147 |
+
streamer=streamer,
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# run on a worker thread so we can yield tokens live
|
| 151 |
+
Thread(target=model.generate, kwargs=gen_kwargs).start()
|
| 152 |
+
|
| 153 |
+
partial = ""
|
| 154 |
+
for token in streamer:
|
| 155 |
+
partial += token
|
| 156 |
+
yield partial, history # 1stΒ outΒ = msg, 2ndΒ outΒ = state
|
| 157 |
+
|
| 158 |
+
# once finished, commit assistant reply to history
|
| 159 |
+
history.append({"role": "assistant", "content": partial})
|
| 160 |
+
yield partial, history
|
| 161 |
+
|
| 162 |
+
# ββββββββββββββββββββββββββββ feedback handler βββββββββββββββββββββββββββββ
|
| 163 |
+
|
| 164 |
+
def submit_feedback(conversation_state: list[dict[str, str]],
|
| 165 |
+
satisfaction: str,
|
| 166 |
+
feedback_text: str):
|
| 167 |
+
"""Callback for the *Submit Research Feedback* button."""
|
| 168 |
+
save_feedback_locally(conversation_state, satisfaction, feedback_text)
|
| 169 |
+
pushed = push_feedback_to_hub()
|
| 170 |
+
if pushed:
|
| 171 |
+
return "β
Thanks!Β Your feedback is safely stored."
|
| 172 |
+
return "β οΈ Saved locally; Hub push failed. Check server logs."
|
| 173 |
+
|
| 174 |
+
# ββββββββββββββββββββββββββββ UI layout ββββββββββββββββββββββββββββββββββββ
|
| 175 |
+
|
| 176 |
+
with gr.Blocks(title="Marinβ8B Research Preview") as demo:
|
| 177 |
+
# state object to surface chat history to the feedback form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
conversation_state = gr.State([])
|
| 179 |
+
|
| 180 |
with gr.Row():
|
| 181 |
+
# βββ Chat column βββ
|
| 182 |
with gr.Column(scale=3):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
chatbot = gr.ChatInterface(
|
| 184 |
+
fn=generate_response,
|
| 185 |
+
additional_inputs=[conversation_state, # keeps state in sync
|
| 186 |
+
gr.Slider(0.1, 2.0, value=0.7, step=0.1,
|
| 187 |
+
label="Temperature"),
|
| 188 |
+
gr.Slider(0.1, 1.0, value=0.9, step=0.05,
|
| 189 |
+
label="TopβP")],
|
| 190 |
additional_outputs=[conversation_state],
|
| 191 |
+
type="messages",
|
| 192 |
)
|
| 193 |
+
|
| 194 |
+
# βββ Sidebar column βββ
|
| 195 |
with gr.Column(scale=1):
|
| 196 |
+
report_btn = gr.Button("Share Feedback", variant="primary")
|
| 197 |
+
|
| 198 |
+
# feedback modal (hidden by default)
|
| 199 |
+
with Modal(visible=False) as fb_modal:
|
| 200 |
+
gr.Markdown("## Research Preview Feedback")
|
| 201 |
+
gr.Markdown("We appreciate your help improving Marinβ8B! β¨")
|
| 202 |
+
|
| 203 |
+
sat_radio = gr.Radio([
|
| 204 |
+
"Very satisfied", "Satisfied", "Neutral",
|
| 205 |
+
"Unsatisfied", "Very unsatisfied"],
|
| 206 |
+
label="Overall experience",
|
| 207 |
+
value="Neutral",
|
| 208 |
+
)
|
| 209 |
+
fb_text = gr.Textbox(lines=6, label="Comments / suggestions")
|
| 210 |
+
send_btn = gr.Button("Submit", variant="primary")
|
| 211 |
+
status_box = gr.Textbox(label="Status", interactive=False)
|
| 212 |
+
|
| 213 |
+
# interactions
|
| 214 |
+
report_btn.click(lambda: None, None, None, _js="() => window.modal_open()")
|
| 215 |
+
# the JS helper above relies on gradioβmodalβs injected helper.
|
| 216 |
+
|
| 217 |
+
send_btn.click(
|
| 218 |
+
submit_feedback,
|
| 219 |
+
inputs=[conversation_state, sat_radio, fb_text],
|
| 220 |
+
outputs=status_box,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
)
|
| 222 |
|
| 223 |
+
# ββββββββββββββββββββββββββββ run! βββββββββββββββββββββββββββββββββββββββββ
|
| 224 |
+
if __name__ == "__main__":
|
| 225 |
+
demo.launch()
|