Spaces:
Sleeping
Sleeping
File size: 5,505 Bytes
e9b0f27 67338ec e9b0f27 4d0c4fb e9b0f27 5036309 8cd7f6d 8114a9d 8cd7f6d 04440b1 5036309 8cd7f6d e8e90f9 8cd7f6d e8e90f9 8cd7f6d | 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 | import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration
tokenizer = T5Tokenizer.from_pretrained("bensglaser/metricalT5-large", legacy=False)
model = T5ForConditionalGeneration.from_pretrained("bensglaser/metricalT5-large")
def transform_to_meter(text):
input_text = f"transform to meter: {text}"
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
outputs = model.generate(**inputs, max_length=128)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
return result
def transform_to_rhythm(text):
input_text = f"transform to rhythm: {text}"
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
outputs = model.generate(**inputs, max_length=128)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
return result
def predict_complexity(text):
input_text = f"predict complexity: {text}"
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
outputs = model.generate(**inputs, max_length=128)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
return result
def run_all_transformations(text):
meter = transform_to_meter(text)
rhythm = transform_to_rhythm(text)
complexity = predict_complexity(text)
combined = f"**Rhythm:**\n{rhythm}\n\n**Original Text:**\n{text}\n\n**Meter:**\n{meter}\n\n**Complexity:**\n{complexity}"
return combined
def create_aligned_table(text):
"""Create an aligned table with rhythm, original text, and meter"""
meter = transform_to_meter(text)
rhythm = transform_to_rhythm(text)
# Split text into syllables (simple word-based splitting for now)
words = text.split()
rhythm_chars = list(rhythm.replace(" ", ""))
meter_chars = list(meter.replace(" ", ""))
# Build aligned rows
rhythm_row = []
text_row = []
meter_row = []
rhythm_idx = 0
meter_idx = 0
for word in words:
# For each word, try to align rhythm and meter characters
word_len = len(word)
# Get rhythm chars for this word
rhythm_segment = ""
for _ in range(word_len):
if rhythm_idx < len(rhythm_chars):
rhythm_segment += rhythm_chars[rhythm_idx]
rhythm_idx += 1
# Get meter chars for this word
meter_segment = ""
for _ in range(word_len):
if meter_idx < len(meter_chars):
meter_segment += meter_chars[meter_idx]
meter_idx += 1
# Pad to match word length
rhythm_segment = rhythm_segment.ljust(word_len)
meter_segment = meter_segment.ljust(word_len)
rhythm_row.append(rhythm_segment)
text_row.append(word)
meter_row.append(meter_segment)
# Join with spaces
rhythm_line = " ".join(rhythm_row)
text_line = " ".join(text_row)
meter_line = " ".join(meter_row)
# Create HTML table
html_table = f"""
<table style="font-family: monospace; border-collapse: collapse; width: 100%;">
<tr style="border-bottom: 1px solid #ddd;">
<td style="padding: 8px;"><strong>Rhythm:</strong></td>
<td style="padding: 8px;">{rhythm_line}</td>
</tr>
<tr style="border-bottom: 1px solid #ddd;">
<td style="padding: 8px;"><strong>Original:</strong></td>
<td style="padding: 8px;">{text_line}</td>
</tr>
<tr>
<td style="padding: 8px;"><strong>Meter:</strong></td>
<td style="padding: 8px;">{meter_line}</td>
</tr>
</table>
"""
return html_table
# Create Gradio interface with tabs
with gr.Blocks() as demo:
gr.Markdown("# MetricalT5 Model")
gr.Markdown("Analyze poetic meter, rhythm, and complexity using T5")
with gr.Tab("Transform to Meter"):
meter_input = gr.Textbox(lines=5, placeholder="Enter text here...")
meter_output = gr.Textbox(label="Meter Analysis")
meter_button = gr.Button("Analyze Meter")
meter_button.click(transform_to_meter, inputs=meter_input, outputs=meter_output)
with gr.Tab("Transform to Rhythm"):
rhythm_input = gr.Textbox(lines=5, placeholder="Enter text here...")
rhythm_output = gr.Textbox(label="Rhythm Analysis")
rhythm_button = gr.Button("Analyze Rhythm")
rhythm_button.click(transform_to_rhythm, inputs=rhythm_input, outputs=rhythm_output)
with gr.Tab("Predict Complexity"):
complexity_input = gr.Textbox(lines=5, placeholder="Enter text here...")
complexity_output = gr.Textbox(label="Complexity Prediction")
complexity_button = gr.Button("Predict Complexity")
complexity_button.click(predict_complexity, inputs=complexity_input, outputs=complexity_output)
with gr.Tab("All Transformations"):
all_input = gr.Textbox(lines=5, placeholder="Enter text here...")
all_output = gr.Textbox(label="All Results", lines=10)
all_button = gr.Button("Run All Transformations")
all_button.click(run_all_transformations, inputs=all_input, outputs=all_output)
with gr.Tab("Aligned View"):
aligned_input = gr.Textbox(lines=5, placeholder="Enter text here...")
aligned_output = gr.HTML(label="Aligned Table")
aligned_button = gr.Button("Generate Aligned View")
aligned_button.click(create_aligned_table, inputs=aligned_input, outputs=aligned_output)
demo.launch()
|