Gabriele Tuccio commited on
Commit
2612b45
·
1 Parent(s): 48a5228

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -25
app.py CHANGED
@@ -418,7 +418,28 @@ def run_grammarllm(prompt, productions_json, regex_json):
418
  except Exception as e:
419
  return f"Errore durante l'inferenza: {str(e)}", None
420
 
421
- # Input di esempio per regex_json (stringa JSON)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  default_regex_json = json.dumps({
423
  "regex_alfanum": "[a-zA-Z0-9]+",
424
  "regex_letters": "[a-zA-Z]+",
@@ -429,31 +450,77 @@ default_regex_json = json.dumps({
429
  "regex_(": "\\("
430
  }, indent=4)
431
 
432
- default_grammar_json = json.dumps({
433
- "S*": ["<<positive>> A", "<<negative>> B", "<<neutral>> C"],
434
- "A": ["<<happy>> D", "<<peaceful>> E", "<<joyful>> F"],
435
- "B": ["<<sad>>", "<<angry>>", "<<frustrated>>"],
436
- "C": ["<<calm>>", "<<indifferent>>", "<<unemotional>>"],
437
- "D": ["<<enthusiastic>>"],
438
- "E": ["<<content>>"],
439
- "F": ["<<excited>>"]
440
- }, indent=4)
441
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442
 
443
- demo = gr.Interface(
444
- fn=run_grammarllm,
445
- inputs=[
446
- gr.Textbox(label="Inserisci prompt testuale"),
447
- gr.Textbox(label="Inserisci productions (JSON)", lines=10, value=default_grammar_json),
448
- gr.Textbox(label="Inserisci regex_dict (JSON)", lines=10, value=default_regex_json),
449
- ],
450
- outputs=[
451
- gr.Textbox(label="Output generato"),
452
- gr.File(label="Scarica ZIP"),
453
- ],
454
- title="GrammarLLM con output e download ZIP",
455
- description="Inserisci prompt, productions e regex per generare testo e scaricare i file.",
456
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457
 
458
  if __name__ == "__main__":
459
- demo.launch(debug=True)
 
418
  except Exception as e:
419
  return f"Errore durante l'inferenza: {str(e)}", None
420
 
421
+ default_grammars = {
422
+ "Default Grammar": json.dumps({
423
+ "S*": ["<<positive>> A", "<<negative>> B", "<<neutral>> C"],
424
+ "A": ["<<happy>> D", "<<peaceful>> E", "<<joyful>> F"],
425
+ "B": ["<<sad>>", "<<angry>>", "<<frustrated>>"],
426
+ "C": ["<<calm>>", "<<indifferent>>", "<<unemotional>>"],
427
+ "D": ["<<enthusiastic>>"],
428
+ "E": ["<<content>>"],
429
+ "F": ["<<excited>>"]
430
+ }, indent=4),
431
+
432
+ "Other example": json.dumps({
433
+ 'S*': ["<<(>> A B", "<<negligent>> V", '<<indifferent>>'],
434
+ 'A': ["number", "letters", "ε"],
435
+ 'B': ['<<)>> letters R'],
436
+ 'R': ['C', 'D'],
437
+ 'C': ['<<calm>>', '<<indifferent>>', '<<unemotional>>'],
438
+ 'D': ['<<angry>>', '<<frustrated>>'],
439
+ 'V': ["<<option>>"],
440
+ }, indent=4),
441
+ }
442
+
443
  default_regex_json = json.dumps({
444
  "regex_alfanum": "[a-zA-Z0-9]+",
445
  "regex_letters": "[a-zA-Z]+",
 
450
  "regex_(": "\\("
451
  }, indent=4)
452
 
 
 
 
 
 
 
 
 
 
453
 
454
+ def update_productions(grammar_choice):
455
+ # Aggiorna textbox productions al cambio preset
456
+ return default_grammars[grammar_choice]
457
+
458
+
459
+ def load_file(file_obj):
460
+ if file_obj is None:
461
+ return ""
462
+ try:
463
+ content = file_obj.read().decode("utf-8")
464
+ # opzionale: validare JSON?
465
+ json.loads(content)
466
+ return content
467
+ except Exception as e:
468
+ return f"Errore nel caricamento file: {str(e)}"
469
+
470
+
471
+ with gr.Blocks() as demo:
472
+
473
+ prompt_input = gr.Textbox(label="Inserisci prompt testuale")
474
+
475
+ with gr.Row():
476
+ grammar_choice = gr.Dropdown(
477
+ list(default_grammars.keys()),
478
+ label="Scegli Productions (JSON)",
479
+ value="Default Grammar",
480
+ interactive=True,
481
+ elem_id="grammar_choice"
482
+ )
483
+ productions_upload = gr.File(label="Carica file Productions (JSON)", file_types=['.json'])
484
+
485
+ productions_text = gr.Textbox(label="Productions (JSON)", lines=10, value=default_grammars["Default Grammar"])
486
 
487
+ with gr.Row():
488
+ regex_upload = gr.File(label="Carica file Regex_dict (JSON)", file_types=['.json'])
489
+
490
+ regex_text = gr.Textbox(label="Inserisci regex_dict (JSON)", lines=10, value=default_regex_json)
491
+
492
+ output_text = gr.Textbox(label="Output generato")
493
+ zip_file = gr.File(label="Scarica ZIP")
494
+
495
+ # Callback: quando cambio dropdown, aggiorno productions_text
496
+ grammar_choice.change(
497
+ fn=update_productions,
498
+ inputs=grammar_choice,
499
+ outputs=productions_text,
500
+ )
501
+
502
+ # Callback: quando carico file productions, aggiorno productions_text (override dropdown)
503
+ productions_upload.upload(
504
+ fn=load_file,
505
+ inputs=productions_upload,
506
+ outputs=productions_text,
507
+ )
508
+
509
+ # Callback: quando carico file regex, aggiorno regex_text
510
+ regex_upload.upload(
511
+ fn=load_file,
512
+ inputs=regex_upload,
513
+ outputs=regex_text,
514
+ )
515
+
516
+ # Al submit del form chiamo run_grammarllm
517
+ submit_btn = gr.Button("Genera output")
518
+
519
+ submit_btn.click(
520
+ fn=run_grammarllm,
521
+ inputs=[prompt_input, productions_text, regex_text],
522
+ outputs=[output_text, zip_file],
523
+ )
524
 
525
  if __name__ == "__main__":
526
+ demo.launch()