akhil commited on
Commit ·
194ec3d
1
Parent(s): 9065be8
reverting to basic version + 1
Browse files
app.py
CHANGED
|
@@ -1,34 +1,46 @@
|
|
| 1 |
from transformers import T5Tokenizer, T5ForConditionalGeneration, pipeline
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# Load the fine-tuned model and tokenizer from the Hugging Face Hub
|
| 6 |
model_dir = "./model" # Replace with your Hugging Face model repo path
|
| 7 |
print(f"Loading model from {model_dir}...")
|
| 8 |
tokenizer = T5Tokenizer.from_pretrained(model_dir)
|
| 9 |
model = T5ForConditionalGeneration.from_pretrained(model_dir)
|
| 10 |
-
|
|
|
|
| 11 |
print("Model and tokenizer loaded successfully.")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Function to paraphrase input texts
|
| 14 |
def paraphrase_text(text):
|
|
|
|
| 15 |
paraphrased_text = ""
|
|
|
|
| 16 |
print(f"Paraphrasing text: {text[:50]}...") # Print the first 50 characters
|
|
|
|
| 17 |
while len(text) > 0:
|
| 18 |
output = rephrase_pipeline(
|
| 19 |
text,
|
| 20 |
-
max_length=
|
| 21 |
-
min_length=
|
| 22 |
-
num_beams=5,
|
| 23 |
early_stopping=True,
|
| 24 |
no_repeat_ngram_size=2,
|
| 25 |
-
temperature=0.
|
| 26 |
-
top_k=
|
| 27 |
-
top_p=0.
|
| 28 |
)
|
| 29 |
-
chunk = output[0]['generated_text']
|
| 30 |
paraphrased_text += chunk
|
| 31 |
text = text[len(chunk):].strip() # Remove the part of text that was paraphrased
|
|
|
|
| 32 |
print(f"Paraphrased text: {paraphrased_text[:50]}...") # Print the first 50 characters of the paraphrased text
|
| 33 |
return paraphrased_text
|
| 34 |
|
|
@@ -38,9 +50,9 @@ iface = gr.Interface(
|
|
| 38 |
inputs="text",
|
| 39 |
outputs="text",
|
| 40 |
title="Paraphrasing Model",
|
| 41 |
-
description="Enter your text to get a paraphrased output in a professional tone."
|
| 42 |
)
|
| 43 |
|
| 44 |
# Launch the Gradio app
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
iface.launch()
|
|
|
|
| 1 |
from transformers import T5Tokenizer, T5ForConditionalGeneration, pipeline
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
# Load the fine-tuned model and tokenizer from the Hugging Face Hub
|
| 6 |
model_dir = "./model" # Replace with your Hugging Face model repo path
|
| 7 |
print(f"Loading model from {model_dir}...")
|
| 8 |
tokenizer = T5Tokenizer.from_pretrained(model_dir)
|
| 9 |
model = T5ForConditionalGeneration.from_pretrained(model_dir)
|
| 10 |
+
device = 0 if torch.cuda.is_available() else -1 # Use GPU if available
|
| 11 |
+
rephrase_pipeline = pipeline('text2text-generation', model=model, tokenizer=tokenizer, device=device)
|
| 12 |
print("Model and tokenizer loaded successfully.")
|
| 13 |
|
| 14 |
+
# Function to calculate dynamic max_length and min_length based on input length
|
| 15 |
+
def calculate_dynamic_lengths(input_text):
|
| 16 |
+
input_length = len(input_text.split()) # Use word count as a proxy for length
|
| 17 |
+
max_length = int(input_length * 1.2) # Set max_length to be 20% longer than input
|
| 18 |
+
min_length = max(50, input_length) # Ensure min_length is at least the input length
|
| 19 |
+
return max_length, min_length
|
| 20 |
+
|
| 21 |
# Function to paraphrase input texts
|
| 22 |
def paraphrase_text(text):
|
| 23 |
+
max_length, min_length = calculate_dynamic_lengths(text)
|
| 24 |
paraphrased_text = ""
|
| 25 |
+
|
| 26 |
print(f"Paraphrasing text: {text[:50]}...") # Print the first 50 characters
|
| 27 |
+
|
| 28 |
while len(text) > 0:
|
| 29 |
output = rephrase_pipeline(
|
| 30 |
text,
|
| 31 |
+
max_length=max_length,
|
| 32 |
+
min_length=min_length,
|
| 33 |
+
num_beams=5 if len(text) < 200 else 10, # Dynamic beam size based on input length
|
| 34 |
early_stopping=True,
|
| 35 |
no_repeat_ngram_size=2,
|
| 36 |
+
temperature=0.7, # Adjusted for more controlled generation
|
| 37 |
+
top_k=40,
|
| 38 |
+
top_p=0.9 # Balanced sampling
|
| 39 |
)
|
| 40 |
+
chunk = output[0]['generated_text']
|
| 41 |
paraphrased_text += chunk
|
| 42 |
text = text[len(chunk):].strip() # Remove the part of text that was paraphrased
|
| 43 |
+
|
| 44 |
print(f"Paraphrased text: {paraphrased_text[:50]}...") # Print the first 50 characters of the paraphrased text
|
| 45 |
return paraphrased_text
|
| 46 |
|
|
|
|
| 50 |
inputs="text",
|
| 51 |
outputs="text",
|
| 52 |
title="Paraphrasing Model",
|
| 53 |
+
description="Enter your text to get a paraphrased output in a professional tone."
|
| 54 |
)
|
| 55 |
|
| 56 |
# Launch the Gradio app
|
| 57 |
if __name__ == "__main__":
|
| 58 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|