Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +60 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
|
| 6 |
+
# Define your custom model class
|
| 7 |
+
class BigramLanguageModel(nn.Module):
|
| 8 |
+
def __init__(self):
|
| 9 |
+
super().__init__()
|
| 10 |
+
# Example layers (adjust as needed for your model)
|
| 11 |
+
self.token_embedding_table = nn.Embedding(61, 64)
|
| 12 |
+
self.position_embedding_table = nn.Embedding(32, 64)
|
| 13 |
+
self.blocks = nn.Sequential(*[nn.Linear(64, 64) for _ in range(4)])
|
| 14 |
+
self.ln_f = nn.LayerNorm(64)
|
| 15 |
+
self.lm_head = nn.Linear(64, 61)
|
| 16 |
+
|
| 17 |
+
def forward(self, idx):
|
| 18 |
+
# Implement the forward pass
|
| 19 |
+
pass
|
| 20 |
+
|
| 21 |
+
def generate(self, idx, max_new_tokens=250):
|
| 22 |
+
# Implement the generate method
|
| 23 |
+
pass
|
| 24 |
+
|
| 25 |
+
# Load your model
|
| 26 |
+
def load_model():
|
| 27 |
+
model = BigramLanguageModel()
|
| 28 |
+
model_url = "https://huggingface.co/yoonusajwardapiit/triptuner/resolve/main/pytorch_model.bin"
|
| 29 |
+
model_weights = torch.hub.load_state_dict_from_url(model_url, map_location=torch.device('cpu'), weights_only=True)
|
| 30 |
+
model.load_state_dict(model_weights)
|
| 31 |
+
model.eval()
|
| 32 |
+
return model
|
| 33 |
+
|
| 34 |
+
model = load_model()
|
| 35 |
+
|
| 36 |
+
# Define encode and decode functions
|
| 37 |
+
chars = sorted(list(set("your_training_text_here"))) # Replace with the character set used in training
|
| 38 |
+
stoi = {ch: i for i, ch in enumerate(chars)}
|
| 39 |
+
itos = {i: ch for i, ch in enumerate(chars)}
|
| 40 |
+
encode = lambda s: [stoi[c] for c in s]
|
| 41 |
+
decode = lambda l: ''.join([itos[i] for i in l])
|
| 42 |
+
|
| 43 |
+
# Function to generate text using the model
|
| 44 |
+
def generate_text(prompt):
|
| 45 |
+
context = torch.tensor([encode(prompt)], dtype=torch.long)
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
generated = model.generate(context, max_new_tokens=250) # Adjust as needed
|
| 48 |
+
return decode(generated[0].tolist())
|
| 49 |
+
|
| 50 |
+
# Create a Gradio interface
|
| 51 |
+
interface = gr.Interface(
|
| 52 |
+
fn=generate_text,
|
| 53 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a location or prompt..."),
|
| 54 |
+
outputs="text",
|
| 55 |
+
title="Triptuner Model",
|
| 56 |
+
description="Generate itineraries for locations in Sri Lanka's Central Province."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Launch the interface
|
| 60 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
gradio
|