coffee334 commited on
Commit
a160eab
ยท
verified ยท
1 Parent(s): bacde10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -54
app.py CHANGED
@@ -1,69 +1,30 @@
1
- import torch
 
2
  import streamlit as st
3
- from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
4
- from datasets import load_dataset
5
 
6
- st.set_page_config(page_title="AstroChat", layout="centered")
 
 
 
 
 
 
7
 
8
- # โœ… Load dataset (WikiText)
9
- dataset = load_dataset("wikitext", "wikitext-103-raw-v1")
10
 
11
- # โœ… Load tokenizer & model (DistilGPT-2)
12
  model_name = "distilgpt2"
13
  tokenizer = AutoTokenizer.from_pretrained(model_name)
14
  model = AutoModelForCausalLM.from_pretrained(model_name)
15
 
16
- # โœ… Ensure tokenizer has a padding token
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
- print(f"Using device: {device}")
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 on WikiText using DistilGPT-2.")
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"):