akhil commited on
Commit ·
194ec3d
1
Parent(s): 9065be8
reverting to basic version + 1
Browse files123456789101112131415161718192021222324252627282930313233343 unmodified lines383940414243444546from transformers import T5Tokenizer, T5ForConditionalGeneration, pipelineimport gradio as grimport os# Load the fine-tuned model and tokenizer from the Hugging Face Hubmodel_dir = "./model" # Replace with your Hugging Face model repo pathprint(f"Loading model from {model_dir}...")tokenizer = T5Tokenizer.from_pretrained(model_dir)model = T5ForConditionalGeneration.from_pretrained(model_dir)rephrase_pipeline = pipeline('text2text-generation', model=model, tokenizer=tokenizer)print("Model and tokenizer loaded successfully.")# Function to paraphrase input textsdef paraphrase_text(text):paraphrased_text = ""print(f"Paraphrasing text: {text[:50]}...") # Print the first 50 characterswhile len(text) > 0:output = rephrase_pipeline(text,max_length=512,min_length=100,num_beams=5,early_stopping=True,no_repeat_ngram_size=2,temperature=0.8,top_k=50,top_p=0.95)chunk = output[0]['generated_text'] # Handle incomplete paraphrasingparaphrased_text += chunktext = text[len(chunk):].strip() # Remove the part of text that was paraphrasedprint(f"Paraphrased text: {paraphrased_text[:50]}...") # Print the first 50 characters of the paraphrased textreturn paraphrased_text3 unmodified linesinputs="text",outputs="text",title="Paraphrasing Model",description="Enter your text to get a paraphrased output in a professional tone.",)# Launch the Gradio appif __name__ == "__main__":iface.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445463 unmodified lines505152535455565758from transformers import T5Tokenizer, T5ForConditionalGeneration, pipelineimport gradio as grimport torch# Load the fine-tuned model and tokenizer from the Hugging Face Hubmodel_dir = "./model" # Replace with your Hugging Face model repo pathprint(f"Loading model from {model_dir}...")tokenizer = T5Tokenizer.from_pretrained(model_dir)model = T5ForConditionalGeneration.from_pretrained(model_dir)device = 0 if torch.cuda.is_available() else -1 # Use GPU if availablerephrase_pipeline = pipeline('text2text-generation', model=model, tokenizer=tokenizer, device=device)print("Model and tokenizer loaded successfully.")# Function to calculate dynamic max_length and min_length based on input lengthdef calculate_dynamic_lengths(input_text):input_length = len(input_text.split()) # Use word count as a proxy for lengthmax_length = int(input_length * 1.2) # Set max_length to be 20% longer than inputmin_length = max(50, input_length) # Ensure min_length is at least the input lengthreturn max_length, min_length# Function to paraphrase input textsdef paraphrase_text(text):max_length, min_length = calculate_dynamic_lengths(text)paraphrased_text = ""print(f"Paraphrasing text: {text[:50]}...") # Print the first 50 characterswhile len(text) > 0:output = rephrase_pipeline(text,max_length=max_length,min_length=min_length,num_beams=5 if len(text) < 200 else 10, # Dynamic beam size based on input lengthearly_stopping=True,no_repeat_ngram_size=2,temperature=0.7, # Adjusted for more controlled generationtop_k=40,top_p=0.9 # Balanced sampling)chunk = output[0]['generated_text']paraphrased_text += chunktext = text[len(chunk):].strip() # Remove the part of text that was paraphrasedprint(f"Paraphrased text: {paraphrased_text[:50]}...") # Print the first 50 characters of the paraphrased textreturn paraphrased_text3 unmodified linesinputs="text",outputs="text",title="Paraphrasing Model",description="Enter your text to get a paraphrased output in a professional tone.")# Launch the Gradio appif __name__ == "__main__":iface.launch(server_name="0.0.0.0", server_port=7860)
app.py+23-11