Spaces:
Sleeping
Sleeping
Update app.py
Browse files17 unmodified lines1819202122232425262728293031323334353637383940414243444546474849501 unmodified line5253545556575859606162636417 unmodified linesmatch = re.search(r'\b\d{1,15}([.,]\d{1,15})?\b', input_text)return match.group(0) if match else "Not found" # Keep as stringdef clean_model_output(model_output):"""Ensures the model's output is valid JSON."""try:model_output = model_output.strip() # Remove extra spaces or newlinesreturn json.loads(model_output) # Convert to dictionaryexcept json.JSONDecodeError:return {} # Return empty dictionary if parsing failsdef generate_command(input_command):"""Runs the model and extracts information."""input_ids = tokenizer("extract: " + input_command, return_tensors="pt").input_ids.to(device)output_ids = model.generate(input_ids, max_length=128, num_beams=1, early_stopping=True)model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)# Extract amount manuallyextracted_amount = extract_amount(input_command)# Parse the model output correctlymodel_data = clean_model_output(model_output)# Extract required variables from model outputaction = model_data.get("action", "Not found")currency = model_data.get("currency", "Not found")recipient = model_data.get("recipient", "Not found")return extracted_amount, action, currency, recipient# Gradio Interfaceiface = gr.Interface(1 unmodified lineinputs=gr.Textbox(lines=2, placeholder="Enter a command..."),outputs=[gr.Textbox(label="Extracted Amount"),gr.Textbox(label="Action"),gr.Textbox(label="Currency"),gr.Textbox(label="Recipient"),],title="Command Extractor",description="Extracts amount separately and displays action, currency, and recipient from model output.")if __name__ == "__main__":iface.launch()17 unmodified lines18192021222324252627282930313233341 unmodified line36373839404142434445464717 unmodified linesmatch = re.search(r'\b\d{1,15}([.,]\d{1,15})?\b', input_text)return match.group(0) if match else "Not found" # Keep as stringdef generate_command(input_command):"""Runs the model and extracts information, also displaying the raw model output for debugging."""input_ids = tokenizer("extract: " + input_command, return_tensors="pt").input_ids.to(device)output_ids = model.generate(input_ids, max_length=128, num_beams=1, early_stopping=True)model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True).strip()# Extract amount manuallyextracted_amount = extract_amount(input_command)return extracted_amount, model_output # Show raw model output# Gradio Interfaceiface = gr.Interface(1 unmodified lineinputs=gr.Textbox(lines=2, placeholder="Enter a command..."),outputs=[gr.Textbox(label="Extracted Amount"),gr.Textbox(label="Raw Model Output"), # Display raw output for debugging],title="Debugging Model Output",description="Displays extracted amount and raw model output for debugging.")if __name__ == "__main__":iface.launch()
app.py+7-24