Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,115 +1,26 @@
|
|
| 1 |
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import torch
|
| 4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
-
from huggingface_hub import login
|
| 6 |
-
|
| 7 |
-
# --- Basic Setup ---
|
| 8 |
-
HF_READONLY_API_KEY = os.getenv("HF_READONLY_API_KEY")
|
| 9 |
-
if HF_READONLY_API_KEY:
|
| 10 |
-
login(token=HF_READONLY_API_KEY)
|
| 11 |
-
|
| 12 |
-
SYSTEM_PROMPT = """You are a guardian model evaluating…</explanation>"""
|
| 13 |
-
MODEL_NAME = "Qwen/Qwen3-0.6B"
|
| 14 |
-
|
| 15 |
-
# --- LAZY LOADING SETUP ---
|
| 16 |
-
# We initialize the model and tokenizer as None. They will be loaded on the first call.
|
| 17 |
-
model = None
|
| 18 |
-
tokenizer = None
|
| 19 |
-
|
| 20 |
-
def load_model_and_tokenizer():
|
| 21 |
-
"""
|
| 22 |
-
Loads the model and tokenizer if they haven't been loaded yet.
|
| 23 |
-
This function will only run its main logic once.
|
| 24 |
-
"""
|
| 25 |
-
global model, tokenizer
|
| 26 |
-
if model is None or tokenizer is None:
|
| 27 |
-
print("--- LAZY LOADING: Loading model and tokenizer for the first time... ---")
|
| 28 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 29 |
-
tokenizer.pad_token_id = tokenizer.pad_token_id or tokenizer.eos_token_id
|
| 30 |
-
|
| 31 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 32 |
-
MODEL_NAME,
|
| 33 |
-
device_map="auto",
|
| 34 |
-
torch_dtype=torch.bfloat16
|
| 35 |
-
).eval()
|
| 36 |
-
print("--- Model and tokenizer loaded successfully. ---")
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
for i, rule in enumerate(rules):
|
| 41 |
-
formatted_rules += f"{i + 1}. {rule}\n"
|
| 42 |
-
formatted_rules += "</rules>\n"
|
| 43 |
-
return formatted_rules
|
| 44 |
|
| 45 |
-
|
| 46 |
-
formatted_transcript = f"<transcript>\n{transcript}\n</transcript>\n"
|
| 47 |
-
return formatted_transcript
|
| 48 |
-
|
| 49 |
-
# --- The Main Gradio Function ---
|
| 50 |
-
def compliance_check(rules_text, transcript_text, thinking):
|
| 51 |
-
"""
|
| 52 |
-
The main inference function for the Gradio app.
|
| 53 |
-
It ensures the model is loaded before running inference.
|
| 54 |
-
"""
|
| 55 |
-
try:
|
| 56 |
-
# STEP 1: Ensure the model is loaded. This will only do work on the first run.
|
| 57 |
-
load_model_and_tokenizer()
|
| 58 |
-
|
| 59 |
-
# STEP 2: Your original, robust input validation.
|
| 60 |
-
if not rules_text or not rules_text.strip():
|
| 61 |
-
return "Error: Please provide at least one rule."
|
| 62 |
-
if not transcript_text or not transcript_text.strip():
|
| 63 |
-
return "Error: Please provide a transcript to analyze."
|
| 64 |
-
|
| 65 |
-
# STEP 3: Format the input and generate a response.
|
| 66 |
-
rules = [r.strip() for r in rules_text.split("\n") if r.strip()]
|
| 67 |
-
inp = format_rules(rules) + format_transcript(transcript_text)
|
| 68 |
-
|
| 69 |
-
message = [
|
| 70 |
-
{'role': 'system', 'content': SYSTEM_PROMPT},
|
| 71 |
-
{'role': 'user', 'content': inp}
|
| 72 |
-
]
|
| 73 |
-
prompt = tokenizer.apply_chat_template(message, tokenize=False, add_generation_prompt=True)
|
| 74 |
-
|
| 75 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
output_content = model.generate(
|
| 79 |
-
**inputs,
|
| 80 |
-
max_new_tokens=256,
|
| 81 |
-
pad_token_id=tokenizer.pad_token_id,
|
| 82 |
-
do_sample=True,
|
| 83 |
-
temperature=0.6,
|
| 84 |
-
top_p=0.95,
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
# Decode only the newly generated part of the response.
|
| 88 |
-
output_text = tokenizer.decode(output_content[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 89 |
-
|
| 90 |
-
return output_text.strip()
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
-
#
|
| 98 |
-
# We keep your well-designed interface configuration.
|
| 99 |
demo = gr.Interface(
|
| 100 |
-
fn=
|
| 101 |
-
inputs=
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
],
|
| 106 |
-
outputs=gr.Textbox(label="Compliance Output", lines=10, max_lines=15, show_copy_button=True),
|
| 107 |
-
title="DynaGuard Compliance Checker",
|
| 108 |
-
description="Paste your rules & transcript, then hit Submit. The model will load on the first request, which may take a moment.",
|
| 109 |
-
allow_flagging="never",
|
| 110 |
-
cache_examples=False
|
| 111 |
)
|
| 112 |
|
| 113 |
-
# --- Launch the App ---
|
| 114 |
if __name__ == "__main__":
|
|
|
|
| 115 |
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# Forcefully disable Server-Side Rendering
|
| 4 |
+
os.environ["GRADIO_ENABLE_SSR"] = "0"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
print("Gradio imported. Starting minimal test app.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def minimal_test_function(text_input):
|
| 11 |
+
"""A simple function that cannot fail."""
|
| 12 |
+
print("Test function executed successfully.")
|
| 13 |
+
return f"The app is stable. You entered: '{text_input}'"
|
| 14 |
|
| 15 |
+
# A minimal interface with no complex dependencies
|
|
|
|
| 16 |
demo = gr.Interface(
|
| 17 |
+
fn=minimal_test_function,
|
| 18 |
+
inputs=gr.Textbox(label="Input Text"),
|
| 19 |
+
outputs=gr.Textbox(label="Output"),
|
| 20 |
+
title="Environment Stability Test",
|
| 21 |
+
description="If you see this page and can use it without it crashing, the environment is now fixed."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
|
|
|
| 24 |
if __name__ == "__main__":
|
| 25 |
+
# Standard launch command
|
| 26 |
demo.launch()
|