Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,16 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
import pandas as pd
|
| 4 |
-
from torch.utils.data import Dataset
|
| 5 |
from sklearn.model_selection import train_test_split
|
| 6 |
-
from fastapi import FastAPI
|
| 7 |
from pydantic import BaseModel
|
| 8 |
-
from
|
|
|
|
| 9 |
import os
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
url = "https://drive.google.com/uc?id=1RCZShB5ohy1HdU-mogcP16TbeVv9txpY"
|
| 13 |
-
df = pd.read_csv(url)
|
| 14 |
-
|
| 15 |
-
# Tokenizer
|
| 16 |
class ScratchTokenizer:
|
| 17 |
-
def
|
| 18 |
self.word2idx = {"<PAD>": 0, "<SOS>": 1, "<EOS>": 2, "<UNK>": 3}
|
| 19 |
self.idx2word = {0: "<PAD>", 1: "<SOS>", 2: "<EOS>", 3: "<UNK>"}
|
| 20 |
self.vocab_size = 4
|
|
@@ -35,34 +31,18 @@ class ScratchTokenizer:
|
|
| 35 |
def decode(self, tokens):
|
| 36 |
return " ".join([self.idx2word.get(idx, "<UNK>") for idx in tokens if idx > 0])
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# Initialize Tokenizer
|
| 42 |
tokenizer = ScratchTokenizer()
|
| 43 |
tokenizer.build_vocab(train_data["instruction"].tolist() + train_data["response"].tolist())
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
class TextDataset(Dataset):
|
| 47 |
-
def _init_(self, data, tokenizer, max_len=200):
|
| 48 |
-
self.data = data
|
| 49 |
-
self.tokenizer = tokenizer
|
| 50 |
-
self.max_len = max_len
|
| 51 |
-
|
| 52 |
-
def _len_(self):
|
| 53 |
-
return len(self.data)
|
| 54 |
-
|
| 55 |
-
def _getitem_(self, idx):
|
| 56 |
-
src_text = self.data.iloc[idx]["instruction"]
|
| 57 |
-
tgt_text = self.data.iloc[idx]["response"]
|
| 58 |
-
src = torch.tensor(self.tokenizer.encode(src_text), dtype=torch.long)
|
| 59 |
-
tgt = torch.tensor(self.tokenizer.encode(tgt_text), dtype=torch.long)
|
| 60 |
-
return src, tgt
|
| 61 |
-
|
| 62 |
-
# Model
|
| 63 |
class GPTModel(nn.Module):
|
| 64 |
-
def
|
| 65 |
-
super(GPTModel, self).
|
| 66 |
self.embedding = nn.Embedding(vocab_size, embed_size)
|
| 67 |
self.pos_embedding = nn.Parameter(torch.randn(1, max_len, embed_size))
|
| 68 |
self.transformer = nn.TransformerDecoder(
|
|
@@ -78,7 +58,7 @@ class GPTModel(nn.Module):
|
|
| 78 |
output = self.transformer(tgt_emb.permute(1, 0, 2), src_emb.permute(1, 0, 2), tgt_mask=tgt_mask)
|
| 79 |
return self.fc_out(output.permute(1, 0, 2))
|
| 80 |
|
| 81 |
-
# Load
|
| 82 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 83 |
model = GPTModel(tokenizer.vocab_size).to(device)
|
| 84 |
|
|
@@ -92,32 +72,34 @@ def load_model(model, path="gpt_model.pth"):
|
|
| 92 |
|
| 93 |
load_model(model)
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
def generate_response(
|
| 97 |
model.eval()
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
return tokenizer.decode(tgt.squeeze(0).tolist())
|
| 107 |
|
| 108 |
-
# FastAPI
|
| 109 |
app = FastAPI()
|
| 110 |
|
| 111 |
-
class
|
| 112 |
query: str
|
| 113 |
|
| 114 |
@app.get("/")
|
| 115 |
-
|
| 116 |
return {"message": "Transformer-based Response Generator API is running!"}
|
| 117 |
|
| 118 |
@app.post("/query")
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
from sklearn.model_selection import train_test_split
|
| 5 |
+
from fastapi import FastAPI, Request
|
| 6 |
from pydantic import BaseModel
|
| 7 |
+
from typing import Optional
|
| 8 |
+
import uvicorn
|
| 9 |
import os
|
| 10 |
|
| 11 |
+
# --- Tokenizer ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class ScratchTokenizer:
|
| 13 |
+
def __init__(self):
|
| 14 |
self.word2idx = {"<PAD>": 0, "<SOS>": 1, "<EOS>": 2, "<UNK>": 3}
|
| 15 |
self.idx2word = {0: "<PAD>", 1: "<SOS>", 2: "<EOS>", 3: "<UNK>"}
|
| 16 |
self.vocab_size = 4
|
|
|
|
| 31 |
def decode(self, tokens):
|
| 32 |
return " ".join([self.idx2word.get(idx, "<UNK>") for idx in tokens if idx > 0])
|
| 33 |
|
| 34 |
+
# --- Load and Prepare Data ---
|
| 35 |
+
url = "https://drive.google.com/uc?id=1RCZShB5ohy1HdU-mogcP16TbeVv9txpY"
|
| 36 |
+
df = pd.read_csv(url)
|
| 37 |
+
train_data, _ = train_test_split(df, test_size=0.2, random_state=42)
|
| 38 |
|
|
|
|
| 39 |
tokenizer = ScratchTokenizer()
|
| 40 |
tokenizer.build_vocab(train_data["instruction"].tolist() + train_data["response"].tolist())
|
| 41 |
|
| 42 |
+
# --- Model ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
class GPTModel(nn.Module):
|
| 44 |
+
def __init__(self, vocab_size, embed_size=256, num_heads=8, num_layers=6, max_len=200):
|
| 45 |
+
super(GPTModel, self).__init__()
|
| 46 |
self.embedding = nn.Embedding(vocab_size, embed_size)
|
| 47 |
self.pos_embedding = nn.Parameter(torch.randn(1, max_len, embed_size))
|
| 48 |
self.transformer = nn.TransformerDecoder(
|
|
|
|
| 58 |
output = self.transformer(tgt_emb.permute(1, 0, 2), src_emb.permute(1, 0, 2), tgt_mask=tgt_mask)
|
| 59 |
return self.fc_out(output.permute(1, 0, 2))
|
| 60 |
|
| 61 |
+
# --- Load Model ---
|
| 62 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 63 |
model = GPTModel(tokenizer.vocab_size).to(device)
|
| 64 |
|
|
|
|
| 72 |
|
| 73 |
load_model(model)
|
| 74 |
|
| 75 |
+
# --- Inference ---
|
| 76 |
+
def generate_response(query, max_length=200):
|
| 77 |
model.eval()
|
| 78 |
+
with torch.no_grad():
|
| 79 |
+
src = torch.tensor(tokenizer.encode(query)).unsqueeze(0).to(device)
|
| 80 |
+
tgt = torch.tensor([[1]]).to(device) # <SOS>
|
| 81 |
+
for _ in range(max_length):
|
| 82 |
+
output = model(src, tgt)
|
| 83 |
+
next_word = output.argmax(-1)[:, -1].unsqueeze(1)
|
| 84 |
+
tgt = torch.cat([tgt, next_word], dim=1)
|
| 85 |
+
if next_word.item() == 2: # <EOS>
|
| 86 |
+
break
|
| 87 |
return tokenizer.decode(tgt.squeeze(0).tolist())
|
| 88 |
|
| 89 |
+
# --- FastAPI App ---
|
| 90 |
app = FastAPI()
|
| 91 |
|
| 92 |
+
class QueryRequest(BaseModel):
|
| 93 |
query: str
|
| 94 |
|
| 95 |
@app.get("/")
|
| 96 |
+
def root():
|
| 97 |
return {"message": "Transformer-based Response Generator API is running!"}
|
| 98 |
|
| 99 |
@app.post("/query")
|
| 100 |
+
def query_model(data: QueryRequest):
|
| 101 |
+
query = data.query.strip()
|
| 102 |
+
if not query:
|
| 103 |
+
return {"error": "Query cannot be empty"}
|
| 104 |
+
response = generate_response(query)
|
| 105 |
+
return {"query": query, "response": response}
|