AjayP13 commited on
Commit
f6fe4f2
·
verified ·
1 Parent(s): 8285b48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -6,7 +6,7 @@ model_name = "google/flan-t5-large"
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
 
9
- def concatenate_and_generate(source_text, target_example_texts, reranking, temperature, top_p):
10
  concatenated_text = source_text + " " + target_example_texts
11
  inputs = tokenizer(concatenated_text, return_tensors="pt")
12
 
@@ -23,8 +23,8 @@ def concatenate_and_generate(source_text, target_example_texts, reranking, tempe
23
  return generated_text
24
 
25
  # Preset examples with cached generations
26
- preset_examples = [
27
- {
28
  "source_text": "Once upon a time in a small village",
29
  "target_example_texts": "In a land far away, there was a kingdom ruled by a wise king. Every day, the people of the kingdom would gather to listen to the king's stories, which were full of wisdom and kindness.",
30
  "reranking": 5,
@@ -32,7 +32,7 @@ preset_examples = [
32
  "top_p": 1.0,
33
  "output": "Once upon a time in a small village in a land far away, there was a kingdom ruled by a wise king. Every day, the people of the kingdom would gather to listen to the king's stories, which were full of wisdom and kindness."
34
  },
35
- {
36
  "source_text": "The quick brown fox",
37
  "target_example_texts": "A nimble, chocolate-colored fox swiftly darted through the emerald forest, weaving between trees with grace and agility.",
38
  "reranking": 5,
@@ -40,13 +40,16 @@ preset_examples = [
40
  "top_p": 0.9,
41
  "output": "The quick brown fox, a nimble, chocolate-colored fox, swiftly darted through the emerald forest, weaving between trees with grace and agility."
42
  }
43
- ]
44
 
45
  # Define Gradio interface
46
  with gr.Blocks(theme="ParityError/[email protected]") as demo:
47
  gr.Markdown("# TinyStyler Demo")
48
  gr.Markdown("Style transfer the source text into the target style, given some example texts of the target style. You can adjust re-ranking and top_p to your desire to control the quality of style transfer. A higher re-ranking value will generally result in better results, at slower speed.")
49
-
 
 
 
50
  source_text = gr.Textbox(lines=3, placeholder="Enter the source text to transform into the target style...", label="Source Text")
51
  target_example_texts = gr.Textbox(lines=5, placeholder="Enter example texts of the target style (one per line)...", label="Example Texts of the Target Style")
52
  reranking = gr.Slider(1, 10, value=5, step=1, label="Re-ranking")
@@ -55,18 +58,17 @@ with gr.Blocks(theme="ParityError/[email protected]") as demo:
55
 
56
  output = gr.Textbox(lines=5, placeholder="Click 'Generate' to transform the source text into the target style.", label="Output", interactive=False)
57
 
58
- def set_example(example):
 
59
  return example["source_text"], example["target_example_texts"], example["reranking"], example["temperature"], example["top_p"], example["output"]
60
 
61
- example_dropdown = gr.Dropdown(label="Preset Examples", choices=[f"Example {i+1}" for i in range(len(preset_examples))])
62
- example_button = gr.Button("Load Example")
63
- example_button.click(
64
- lambda example_index: set_example(preset_examples[int(example_index.split()[-1])-1]),
65
  inputs=[example_dropdown],
66
  outputs=[source_text, target_example_texts, reranking, temperature, top_p, output]
67
  )
68
 
69
  btn = gr.Button("Generate")
70
- btn.click(concatenate_and_generate, [source_text, target_example_texts, reranking, temperature, top_p], output)
71
 
72
  demo.launch()
 
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
 
9
+ def run_tinystyler(source_text, target_example_texts, reranking, temperature, top_p):
10
  concatenated_text = source_text + " " + target_example_texts
11
  inputs = tokenizer(concatenated_text, return_tensors="pt")
12
 
 
23
  return generated_text
24
 
25
  # Preset examples with cached generations
26
+ preset_examples = {
27
+ "Example 1": {
28
  "source_text": "Once upon a time in a small village",
29
  "target_example_texts": "In a land far away, there was a kingdom ruled by a wise king. Every day, the people of the kingdom would gather to listen to the king's stories, which were full of wisdom and kindness.",
30
  "reranking": 5,
 
32
  "top_p": 1.0,
33
  "output": "Once upon a time in a small village in a land far away, there was a kingdom ruled by a wise king. Every day, the people of the kingdom would gather to listen to the king's stories, which were full of wisdom and kindness."
34
  },
35
+ "Example 2": {
36
  "source_text": "The quick brown fox",
37
  "target_example_texts": "A nimble, chocolate-colored fox swiftly darted through the emerald forest, weaving between trees with grace and agility.",
38
  "reranking": 5,
 
40
  "top_p": 0.9,
41
  "output": "The quick brown fox, a nimble, chocolate-colored fox, swiftly darted through the emerald forest, weaving between trees with grace and agility."
42
  }
43
+ }
44
 
45
  # Define Gradio interface
46
  with gr.Blocks(theme="ParityError/[email protected]") as demo:
47
  gr.Markdown("# TinyStyler Demo")
48
  gr.Markdown("Style transfer the source text into the target style, given some example texts of the target style. You can adjust re-ranking and top_p to your desire to control the quality of style transfer. A higher re-ranking value will generally result in better results, at slower speed.")
49
+
50
+ with gr.Row():
51
+ example_dropdown = gr.Dropdown(label="Examples", choices=list(preset_examples.keys()))
52
+
53
  source_text = gr.Textbox(lines=3, placeholder="Enter the source text to transform into the target style...", label="Source Text")
54
  target_example_texts = gr.Textbox(lines=5, placeholder="Enter example texts of the target style (one per line)...", label="Example Texts of the Target Style")
55
  reranking = gr.Slider(1, 10, value=5, step=1, label="Re-ranking")
 
58
 
59
  output = gr.Textbox(lines=5, placeholder="Click 'Generate' to transform the source text into the target style.", label="Output", interactive=False)
60
 
61
+ def set_example(example_name):
62
+ example = preset_examples[example_name]
63
  return example["source_text"], example["target_example_texts"], example["reranking"], example["temperature"], example["top_p"], example["output"]
64
 
65
+ example_dropdown.change(
66
+ set_example,
 
 
67
  inputs=[example_dropdown],
68
  outputs=[source_text, target_example_texts, reranking, temperature, top_p, output]
69
  )
70
 
71
  btn = gr.Button("Generate")
72
+ btn.click(run_tinystyler, [source_text, target_example_texts, reranking, temperature, top_p], output)
73
 
74
  demo.launch()