Spaces:
Build error
Build error
Update app.py
Browse files41 unmodified lines42434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919241 unmodified lines# Step 4: Fine-tune a model on the extracted tweetsdef fine_tune_model(tweets):# Convert tweets to a DataFrame and Datasetdf = pd.DataFrame(tweets, columns=["text"])tweet_dataset = Dataset.from_pandas(df)# Load model and tokenizermodel_name = "gpt2" # Replace with a suitable model if neededtokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name)# Set the pad tokentokenizer.pad_token = tokenizer.eos_token # Use eos_token as pad_token# Tokenize the datasetdef tokenize_function(examples):return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=128)tokenized_tweets = tweet_dataset.map(tokenize_function, batched=True)# Training argumentstraining_args = TrainingArguments(output_dir="./fine_tuned_tweet_model",per_device_train_batch_size=4,num_train_epochs=3,save_steps=10_000,save_total_limit=1,logging_dir='./logs',)# Initialize the Trainertrainer = Trainer(model=model,args=training_args,train_dataset=tokenized_tweets,)# Fine-tune the modeltrainer.train()# Save the fine-tuned modelmodel.save_pretrained("fine_tuned_tweet_model")tokenizer.save_pretrained("fine_tuned_tweet_model")return model, tokenizer# Trigger fine-tuning and notify userwith st.spinner("Fine-tuning model..."):41 unmodified lines42434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888941 unmodified lines# Step 4: Fine-tune a model on the extracted tweetsdef fine_tune_model(tweets):# Convert tweets to a DataFrame and Datasetdf = pd.DataFrame(tweets, columns=["text"])tweet_dataset = Dataset.from_pandas(df)# Load model and tokenizermodel_name = "gpt2" # Replace with a suitable model if neededtokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name)tokenizer.pad_token = tokenizer.eos_token# Tokenize the datasetdef tokenize_function(examples):return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=128)tokenized_tweets = tweet_dataset.map(tokenize_function, batched=True)# Training argumentstraining_args = TrainingArguments(output_dir="./fine_tuned_tweet_model",per_device_train_batch_size=4,num_train_epochs=3,save_steps=10_000,save_total_limit=1,logging_dir='./logs',)# Initialize the Trainertrainer = Trainer(model=model,args=training_args,train_dataset=tokenized_tweets,)# Fine-tune the modeltrainer.train()# Save the fine-tuned modelmodel.save_pretrained("fine_tuned_tweet_model")tokenizer.save_pretrained("fine_tuned_tweet_model")return model, tokenizer# Trigger fine-tuning and notify userwith st.spinner("Fine-tuning model..."):
app.py+27-30