Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,20 @@
|
|
| 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
|
| 6 |
from pydantic import BaseModel
|
| 7 |
-
from
|
| 8 |
-
import uvicorn
|
| 9 |
import os
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class ScratchTokenizer:
|
| 13 |
-
def
|
| 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,18 +35,34 @@ class ScratchTokenizer:
|
|
| 31 |
def decode(self, tokens):
|
| 32 |
return " ".join([self.idx2word.get(idx, "<UNK>") for idx in tokens if idx > 0])
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
class GPTModel(nn.Module):
|
| 44 |
-
def
|
| 45 |
-
super(GPTModel, self).
|
| 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,7 +78,7 @@ class GPTModel(nn.Module):
|
|
| 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 |
-
#
|
| 62 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 63 |
model = GPTModel(tokenizer.vocab_size).to(device)
|
| 64 |
|
|
@@ -72,34 +92,32 @@ def load_model(model, path="gpt_model.pth"):
|
|
| 72 |
|
| 73 |
load_model(model)
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
def generate_response(query, max_length=200):
|
| 77 |
model.eval()
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
break
|
| 87 |
return tokenizer.decode(tgt.squeeze(0).tolist())
|
| 88 |
|
| 89 |
-
#
|
| 90 |
app = FastAPI()
|
| 91 |
|
| 92 |
-
class
|
| 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(
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
return {"query": query, "response": response}
|
|
|
|
| 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 fastapi.responses import JSONResponse
|
|
|
|
| 9 |
import os
|
| 10 |
|
| 11 |
+
# Load data
|
| 12 |
+
url = "https://drive.google.com/uc?id=1RCZShB5ohy1HdU-mogcP16TbeVv9txpY"
|
| 13 |
+
df = pd.read_csv(url)
|
| 14 |
+
|
| 15 |
+
# Tokenizer
|
| 16 |
class ScratchTokenizer:
|
| 17 |
+
def _init_(self):
|
| 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 |
def decode(self, tokens):
|
| 36 |
return " ".join([self.idx2word.get(idx, "<UNK>") for idx in tokens if idx > 0])
|
| 37 |
|
| 38 |
+
# Train-Test Split
|
| 39 |
+
train_data, test_data = train_test_split(df, test_size=0.2, random_state=42)
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Initialize Tokenizer
|
| 42 |
tokenizer = ScratchTokenizer()
|
| 43 |
tokenizer.build_vocab(train_data["instruction"].tolist() + train_data["response"].tolist())
|
| 44 |
|
| 45 |
+
# Dataset Class (not used in inference but useful for training)
|
| 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 _init_(self, vocab_size, embed_size=256, num_heads=8, num_layers=6, max_len=200):
|
| 65 |
+
super(GPTModel, self)._init_()
|
| 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 |
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 model
|
| 82 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 83 |
model = GPTModel(tokenizer.vocab_size).to(device)
|
| 84 |
|
|
|
|
| 92 |
|
| 93 |
load_model(model)
|
| 94 |
|
| 95 |
+
# Generate Response
|
| 96 |
+
def generate_response(model, query, max_length=200):
|
| 97 |
model.eval()
|
| 98 |
+
src = torch.tensor(tokenizer.encode(query)).unsqueeze(0).to(device)
|
| 99 |
+
tgt = torch.tensor([[1]]).to(device) # <SOS>
|
| 100 |
+
for _ in range(max_length):
|
| 101 |
+
output = model(src, tgt)
|
| 102 |
+
next_word = output.argmax(-1)[:, -1].unsqueeze(1)
|
| 103 |
+
tgt = torch.cat([tgt, next_word], dim=1)
|
| 104 |
+
if next_word.item() == 2: # <EOS>
|
| 105 |
+
break
|
|
|
|
| 106 |
return tokenizer.decode(tgt.squeeze(0).tolist())
|
| 107 |
|
| 108 |
+
# FastAPI app
|
| 109 |
app = FastAPI()
|
| 110 |
|
| 111 |
+
class Query(BaseModel):
|
| 112 |
query: str
|
| 113 |
|
| 114 |
@app.get("/")
|
| 115 |
+
async def root():
|
| 116 |
return {"message": "Transformer-based Response Generator API is running!"}
|
| 117 |
|
| 118 |
@app.post("/query")
|
| 119 |
+
async def query_model(query: Query):
|
| 120 |
+
if not query.query.strip():
|
| 121 |
+
return JSONResponse(status_code=400, content={"error": "Query cannot be empty"})
|
| 122 |
+
response = generate_response(model, query.query)
|
| 123 |
+
return {"query": query.query, "response": response}
|
|
|