Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
# app.py
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
-
from fastapi import FastAPI, Request
|
| 5 |
-
from fastapi.responses import JSONResponse
|
| 6 |
from tokenizers import Tokenizer
|
| 7 |
-
import
|
| 8 |
|
| 9 |
# -----------------------------
|
| 10 |
# Load tokenizer
|
|
@@ -14,7 +12,7 @@ tokenizer = Tokenizer.from_file(tokenizer_path)
|
|
| 14 |
vocab_size = tokenizer.get_vocab_size()
|
| 15 |
|
| 16 |
# -----------------------------
|
| 17 |
-
# Define
|
| 18 |
# -----------------------------
|
| 19 |
class SimpleTransformer(nn.Module):
|
| 20 |
def __init__(self, vocab_size, d_model=128, nhead=4, num_layers=4):
|
|
@@ -39,25 +37,28 @@ model.load_state_dict(torch.load("pytorch_model.bin", map_location="cpu"))
|
|
| 39 |
model.eval()
|
| 40 |
|
| 41 |
# -----------------------------
|
| 42 |
-
#
|
| 43 |
# -----------------------------
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
@app.post("/generate")
|
| 47 |
-
async def generate(request: Request):
|
| 48 |
-
data = await request.json()
|
| 49 |
-
prompt = data.get("prompt", "")
|
| 50 |
-
|
| 51 |
-
# Tokenize
|
| 52 |
input_ids = tokenizer.encode(prompt).ids
|
| 53 |
input_tensor = torch.tensor([input_ids])
|
| 54 |
-
|
| 55 |
-
# Generate (basic greedy)
|
| 56 |
with torch.no_grad():
|
| 57 |
output = model(input_tensor)
|
| 58 |
predicted_ids = torch.argmax(output, dim=-1)[0].tolist()
|
| 59 |
-
|
| 60 |
-
# Decode
|
| 61 |
response_text = tokenizer.decode(predicted_ids)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
|
|
|
| 1 |
# app.py
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
|
|
|
|
|
|
| 4 |
from tokenizers import Tokenizer
|
| 5 |
+
import gradio as gr
|
| 6 |
|
| 7 |
# -----------------------------
|
| 8 |
# Load tokenizer
|
|
|
|
| 12 |
vocab_size = tokenizer.get_vocab_size()
|
| 13 |
|
| 14 |
# -----------------------------
|
| 15 |
+
# Define the same transformer as used in training
|
| 16 |
# -----------------------------
|
| 17 |
class SimpleTransformer(nn.Module):
|
| 18 |
def __init__(self, vocab_size, d_model=128, nhead=4, num_layers=4):
|
|
|
|
| 37 |
model.eval()
|
| 38 |
|
| 39 |
# -----------------------------
|
| 40 |
+
# Generation function
|
| 41 |
# -----------------------------
|
| 42 |
+
def generate(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
input_ids = tokenizer.encode(prompt).ids
|
| 44 |
input_tensor = torch.tensor([input_ids])
|
| 45 |
+
|
|
|
|
| 46 |
with torch.no_grad():
|
| 47 |
output = model(input_tensor)
|
| 48 |
predicted_ids = torch.argmax(output, dim=-1)[0].tolist()
|
| 49 |
+
|
|
|
|
| 50 |
response_text = tokenizer.decode(predicted_ids)
|
| 51 |
+
return response_text
|
| 52 |
+
|
| 53 |
+
# -----------------------------
|
| 54 |
+
# Gradio Interface
|
| 55 |
+
# -----------------------------
|
| 56 |
+
iface = gr.Interface(
|
| 57 |
+
fn=generate,
|
| 58 |
+
inputs=gr.Textbox(lines=2, placeholder="Type a prompt for Dave..."),
|
| 59 |
+
outputs="text",
|
| 60 |
+
title="Dave – Fully Custom AI",
|
| 61 |
+
description="Interact with your fully custom AI trained from scratch."
|
| 62 |
+
)
|
| 63 |
|
| 64 |
+
iface.launch()
|