Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,30 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
|
| 4 |
-
from datasets import load_dataset
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
# โ
Load tokenizer & model
|
| 12 |
model_name = "distilgpt2"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 15 |
|
| 16 |
-
# โ
|
| 17 |
-
if tokenizer.pad_token is None:
|
| 18 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 19 |
-
|
| 20 |
-
# โ
Tokenization function
|
| 21 |
-
def tokenize_function(examples):
|
| 22 |
-
return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=128)
|
| 23 |
-
|
| 24 |
-
# โ
Tokenize dataset
|
| 25 |
-
tokenized_datasets = dataset.map(tokenize_function, batched=True, remove_columns=["text"])
|
| 26 |
-
|
| 27 |
-
# โ
Detect GPU automatically
|
| 28 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# โ
Define training arguments
|
| 32 |
-
training_args = TrainingArguments(
|
| 33 |
-
output_dir="./results",
|
| 34 |
-
evaluation_strategy="epoch",
|
| 35 |
-
per_device_train_batch_size=8,
|
| 36 |
-
per_device_eval_batch_size=8,
|
| 37 |
-
num_train_epochs=3,
|
| 38 |
-
save_strategy="epoch",
|
| 39 |
-
save_total_limit=2,
|
| 40 |
-
load_best_model_at_end=True,
|
| 41 |
-
logging_dir="./logs",
|
| 42 |
-
logging_steps=500,
|
| 43 |
-
fp16=True if device == "cuda" else False, # Use mixed precision on GPU
|
| 44 |
-
no_cuda=not torch.cuda.is_available(),
|
| 45 |
-
push_to_hub=True # โ
Upload model to Hugging Face Hub
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
# โ
Trainer setup
|
| 49 |
-
trainer = Trainer(
|
| 50 |
-
model=model,
|
| 51 |
-
args=training_args,
|
| 52 |
-
train_dataset=tokenized_datasets["train"],
|
| 53 |
-
eval_dataset=tokenized_datasets["validation"],
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
# โ
Train the model
|
| 57 |
-
if __name__ == "__main__":
|
| 58 |
-
trainer.train()
|
| 59 |
-
trainer.save_model("./trained_model") # Save trained model
|
| 60 |
-
tokenizer.save_pretrained("./trained_model")
|
| 61 |
|
| 62 |
# โ
Text Generation Function
|
| 63 |
def generate_text(prompt, max_length=150, temperature=0.8, top_p=0.9):
|
| 64 |
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
|
| 65 |
-
model.to(device)
|
| 66 |
-
|
| 67 |
output = model.generate(
|
| 68 |
input_ids,
|
| 69 |
max_length=max_length,
|
|
@@ -75,7 +36,7 @@ def generate_text(prompt, max_length=150, temperature=0.8, top_p=0.9):
|
|
| 75 |
|
| 76 |
# โ
Streamlit UI
|
| 77 |
st.title("๐ฎ AstroChat - AI Chatbot")
|
| 78 |
-
st.write("A conversational AI trained
|
| 79 |
|
| 80 |
user_input = st.text_input("Ask me anything:")
|
| 81 |
if st.button("Generate Response"):
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
import streamlit as st
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# โ
Manually install PyTorch if not found
|
| 6 |
+
try:
|
| 7 |
+
import torch
|
| 8 |
+
except ModuleNotFoundError:
|
| 9 |
+
st.warning("Installing missing dependencies... Please wait.")
|
| 10 |
+
subprocess.run(["pip", "install", "torch", "torchvision", "torchaudio"], check=True)
|
| 11 |
+
import torch # Re-import after installation
|
| 12 |
|
| 13 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 14 |
+
import torch
|
| 15 |
|
| 16 |
+
# โ
Load tokenizer & model
|
| 17 |
model_name = "distilgpt2"
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 20 |
|
| 21 |
+
# โ
Detect GPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
+
model.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# โ
Text Generation Function
|
| 26 |
def generate_text(prompt, max_length=150, temperature=0.8, top_p=0.9):
|
| 27 |
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
|
|
|
|
|
|
|
| 28 |
output = model.generate(
|
| 29 |
input_ids,
|
| 30 |
max_length=max_length,
|
|
|
|
| 36 |
|
| 37 |
# โ
Streamlit UI
|
| 38 |
st.title("๐ฎ AstroChat - AI Chatbot")
|
| 39 |
+
st.write("A conversational AI trained using DistilGPT-2.")
|
| 40 |
|
| 41 |
user_input = st.text_input("Ask me anything:")
|
| 42 |
if st.button("Generate Response"):
|