taruschirag commited on
Commit
202a7c9
·
verified ·
1 Parent(s): d6fa859

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -104
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
- def format_rules(rules):
39
- formatted_rules = "<rules>\n"
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
- def format_transcript(transcript):
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
- with torch.no_grad():
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
- except Exception as e:
93
- # A simple, safe error handler.
94
- print(f"An error occurred: {str(e)}")
95
- return "An error occurred during processing. The application might be under heavy load or encountered a problem. Please try again."
96
 
97
- # --- Build the Gradio Interface ---
98
- # We keep your well-designed interface configuration.
99
  demo = gr.Interface(
100
- fn=compliance_check,
101
- inputs=[
102
- gr.Textbox(lines=5, label="Rules (one per line)", max_lines=10, placeholder="Enter compliance rules, one per line..."),
103
- gr.Textbox(lines=10, label="Transcript", max_lines=15, placeholder="Paste the transcript to analyze..."),
104
- gr.Checkbox(label="Enable ⟨think⟩ mode", value=True)
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()