ecopus commited on
Commit
43ec587
·
verified ·
1 Parent(s): 11f7cc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py CHANGED
@@ -78,6 +78,89 @@ MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
78
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
79
  pipe = pipeline("text-generation", model=MODEL_ID, tokenizer=tokenizer)
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  def llm_explain(record: dict) -> str:
82
  if not record.get("ok", False):
83
  return "Errors: " + "; ".join(record.get("errors", []))
 
78
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
79
  pipe = pipeline("text-generation", model=MODEL_ID, tokenizer=tokenizer)
80
 
81
+ def _fmt_num(x, sig=4):
82
+ """Safe, short formatting for numbers (returns string)."""
83
+ try:
84
+ if x is None:
85
+ return "N/A"
86
+ if isinstance(x, (int,)):
87
+ return str(x)
88
+ if isinstance(x, float):
89
+ # use general format with sig significant digits
90
+ return f"{x:.{sig}g}"
91
+ return str(x)
92
+ except Exception:
93
+ return str(x)
94
+
95
+ def _llm_generate(prompt: str, max_tokens: int = 300) -> str:
96
+ """
97
+ Run the local pipeline, then strip any echoed prompt and common instruction text.
98
+ If the model echoes instructions, do one gentle retry with a simplified prompt.
99
+ """
100
+ # Primary generation: deterministic (no sampling) is usually safer for engineering text.
101
+ try:
102
+ out = pipe(
103
+ prompt,
104
+ max_new_tokens=max_tokens,
105
+ do_sample=False,
106
+ temperature=0.0,
107
+ return_full_text=True,
108
+ )
109
+ except Exception:
110
+ # fallback: try return_full_text=False if first attempt fails for this model
111
+ out = pipe(
112
+ prompt,
113
+ max_new_tokens=max_tokens,
114
+ do_sample=False,
115
+ temperature=0.0,
116
+ return_full_text=False,
117
+ )
118
+
119
+ # get text (handle both pipeline variants)
120
+ text = ""
121
+ if isinstance(out, list) and len(out) > 0:
122
+ text = out[0].get("generated_text", "") or out[0].get("text", "") or ""
123
+ text = text or ""
124
+ # If the model returned the prompt + output, strip the prompt if present
125
+ if text.startswith(prompt):
126
+ text = text[len(prompt):]
127
+ text = text.strip()
128
+
129
+ # If output looks like it merely repeated instructions, try a simpler short-prompt retry
130
+ low_quality_indicators = [
131
+ "Use bullet points", "Be sure to include", "Do not", "Do NOT",
132
+ "Now produce", "System:", "User:", "Instruction:"
133
+ ]
134
+ if (not text) or any(ind in text for ind in low_quality_indicators) or len(text) < 10:
135
+ # simple short retry prompt asking for only the final answer
136
+ simple_prompt = prompt + "\n\nNow produce ONLY the requested explanation below (no re-statement of the prompt or instructions):\n"
137
+ try:
138
+ out2 = pipe(
139
+ simple_prompt,
140
+ max_new_tokens=max_tokens,
141
+ do_sample=False,
142
+ temperature=0.0,
143
+ return_full_text=True,
144
+ )
145
+ except Exception:
146
+ out2 = pipe(
147
+ simple_prompt,
148
+ max_new_tokens=max_tokens,
149
+ do_sample=False,
150
+ temperature=0.0,
151
+ return_full_text=False,
152
+ )
153
+ text2 = out2[0].get("generated_text", "") or out2[0].get("text", "") or ""
154
+ if text2.startswith(simple_prompt):
155
+ text2 = text2[len(simple_prompt):]
156
+ text2 = text2.strip()
157
+ if text2 and len(text2) > 10 and not any(ind in text2 for ind in low_quality_indicators):
158
+ return text2
159
+ # final fallback:
160
+ return "[LLM failed to generate a usable explanation — try a different model or reduce the prompt size]"
161
+
162
+ return text
163
+
164
  def llm_explain(record: dict) -> str:
165
  if not record.get("ok", False):
166
  return "Errors: " + "; ".join(record.get("errors", []))