ghostai1 commited on
Commit
bdbf617
Β·
verified Β·
1 Parent(s): 30098bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -1,25 +1,21 @@
1
  # πŸ”„ Text Paraphraser | CPU-only HF Space
2
 
3
  import gradio as gr
4
- from transformers import (
5
- AutoTokenizer,
6
- AutoModelForSeq2SeqLM,
7
- pipeline,
8
- )
9
 
10
- # 1️⃣ Load model + slow tokenizer explicitly
11
  MODEL_ID = "Vamsi/T5_Paraphrase_Paws"
12
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=False)
13
  model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
14
 
15
- # 2️⃣ Create paraphrase pipeline with our slow tokenizer
16
  paraphraser = pipeline(
17
  "text2text-generation",
18
  model=model,
19
  tokenizer=tokenizer,
20
- device=-1, # CPU
21
  )
22
 
 
23
  def paraphrase(text: str, num_variations: int):
24
  if not text.strip():
25
  return []
@@ -30,10 +26,11 @@ def paraphrase(text: str, num_variations: int):
30
  num_return_sequences=num_variations,
31
  do_sample=True,
32
  top_k=120,
33
- top_p=0.95
34
  )
35
  return [out["generated_text"].strip() for out in outputs]
36
 
 
37
  with gr.Blocks(title="πŸ”„ Text Paraphraser") as demo:
38
  gr.Markdown(
39
  "# πŸ”„ Text Paraphraser\n"
@@ -55,18 +52,18 @@ with gr.Blocks(title="πŸ”„ Text Paraphraser") as demo:
55
  output_df = gr.Dataframe(
56
  label="Paraphrases",
57
  headers=[f"Variant #{i}" for i in range(1, 6)],
58
- datatype=["str"]*5,
59
  interactive=False,
60
  row_count=1
61
  )
62
 
63
  def format_for_dataframe(results):
64
  # Pad out to 5 columns
65
- variants = results + [""]*(5 - len(results))
66
  return [variants]
67
 
68
  run_btn.click(
69
- fn=lambda text, n: format_for_dataframe(paraphrase(text, n)),
70
  inputs=[input_text, variations],
71
  outputs=output_df
72
  )
 
1
  # πŸ”„ Text Paraphraser | CPU-only HF Space
2
 
3
  import gradio as gr
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
 
 
 
 
5
 
6
+ # 1️⃣ Model & Tokenizer setup
7
  MODEL_ID = "Vamsi/T5_Paraphrase_Paws"
8
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=False)
9
  model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
10
 
 
11
  paraphraser = pipeline(
12
  "text2text-generation",
13
  model=model,
14
  tokenizer=tokenizer,
15
+ device=-1, # CPU
16
  )
17
 
18
+ # 2️⃣ Paraphrase function
19
  def paraphrase(text: str, num_variations: int):
20
  if not text.strip():
21
  return []
 
26
  num_return_sequences=num_variations,
27
  do_sample=True,
28
  top_k=120,
29
+ top_p=0.95,
30
  )
31
  return [out["generated_text"].strip() for out in outputs]
32
 
33
+ # 3️⃣ Gradio UI
34
  with gr.Blocks(title="πŸ”„ Text Paraphraser") as demo:
35
  gr.Markdown(
36
  "# πŸ”„ Text Paraphraser\n"
 
52
  output_df = gr.Dataframe(
53
  label="Paraphrases",
54
  headers=[f"Variant #{i}" for i in range(1, 6)],
55
+ datatype=["str"] * 5,
56
  interactive=False,
57
  row_count=1
58
  )
59
 
60
  def format_for_dataframe(results):
61
  # Pad out to 5 columns
62
+ variants = results + [""] * (5 - len(results))
63
  return [variants]
64
 
65
  run_btn.click(
66
+ fn=lambda txt, n: format_for_dataframe(paraphrase(txt, n)),
67
  inputs=[input_text, variations],
68
  outputs=output_df
69
  )