Spaces:
Runtime error
Runtime error
File size: 4,428 Bytes
5062648 8aedac7 3058282 e405859 ea6ef25 c52c0f1 ea6ef25 c52c0f1 ea6ef25 dcfc32f ea6ef25 c52c0f1 1c571f3 c52c0f1 1c571f3 c52c0f1 1c571f3 c52c0f1 1c571f3 c52c0f1 5d7013d c52c0f1 5dd830f 78148b4 c251a2e 78148b4 c52c0f1 58c694a c52c0f1 ea6ef25 c52c0f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import gradio as gr
import torch
from torch import nn
import lightning.pytorch as pl
from torch.nn import functional as F
from utils import GPTLM
newmodel = GPTLM.load_from_checkpoint('shakespeare_gpt.pth')
chars = ['\n', ' ', '!', '$', '&', "'", ',', '-', '.', '3', ':', ';', '?', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
vocab_size = len(chars)
# create a mapping from characters to integers
stoi = { ch:i for i,ch in enumerate(chars) }
itos = { i:ch for i,ch in enumerate(chars) }
encode = lambda s: [stoi[c] for c in s] # encoder: take a string, output a list of integers
decode = lambda l: ''.join([itos[i] for i in l]) # decoder: take a list of integers, output a string
def generate_dialogue(character_dropdown, seed_slider):
if character_dropdown == "NONE":
context = torch.zeros((1, 1), dtype=torch.long)
return decode(newmodel.model.generate(context, max_new_tokens=100)[0].tolist())
else:
context = torch.tensor([encode(character_dropdown)], dtype=torch.long)
return decode(newmodel.model.generate(context, max_new_tokens=100)[0].tolist())
HTML_TEMPLATE = """
<style>
#app-header {
text-align: center;
background: rgba(255, 255, 255, 0.3); /* Semi-transparent white */
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
position: relative; /* To position the artifacts */
}
#app-header h1 {
color: #FF0000;
font-size: 2em;
margin-bottom: 10px;
}
.concept {
position: relative;
transition: transform 0.3s;
}
.concept:hover {
transform: scale(1.1);
}
.concept img {
width: 100px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.concept-description {
position: absolute;
bottom: -30px;
left: 50%;
transform: translateX(-50%);
background-color: #4CAF50;
color: white;
padding: 5px 10px;
border-radius: 5px;
opacity: 0;
transition: opacity 0.3s;
}
.concept:hover .concept-description {
opacity: 1;
}
/* Artifacts */
</style>
<div id="app-header">
<!-- Artifacts -->
<div class="artifact large"></div>
<div class="artifact large"></div>
<div class="artifact large"></div>
<div class="artifact large"></div>
<!-- Content -->
<h1>SHAKESPEARE DIALOGUE GENERATOR</h1>
<p>Generate dialogue for Shakespearean character by selecting character from dropdown.</p>
"""
with gr.Blocks(theme=gr.themes.Glass(),css=".gradio-container {background: url('file=https://github.com/Delve-ERAV1/S20/assets/11761529/c0ff84a4-dde6-473e-a820-d3797040eb9d')}") as interface:
gr.HTML(value=HTML_TEMPLATE, show_label=False)
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
with gr.Row():
character_dropdown = gr.Dropdown(
label="Select a Character",
choices=["NONE","ROMEO","JULIET","MENENIUS","ANTONIO"],
value='Dream'
)
seed_slider = gr.Slider(
label="Random Seed",
minimum=0,
maximum=1000,
step=1,
value=42
)
inputs = [character_dropdown, seed_slider]
with gr.Row():
outputs = gr.Textbox(
label="Generated Dialogue"
)
with gr.Row():
button = gr.Button("Generate Dialogue")
button.click(generate_dialogue, inputs=inputs, outputs=outputs)
if __name__ == "__main__":
interface.launch(enable_queue=True) |