Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
|
|
|
| 4 |
|
| 5 |
-
# 1.
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def convert_to_markdown(input_text):
|
| 11 |
-
|
| 12 |
-
full_input = f"markdown: {input_text}"
|
| 13 |
-
|
| 14 |
-
# 3. Tokenize input
|
| 15 |
-
inputs = tokenizer(full_input, return_tensors="pt", max_length=512, truncation=True)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
demo = gr.Interface(
|
| 26 |
fn=convert_to_markdown,
|
| 27 |
-
inputs=gr.Textbox(lines=5, label="Plain Text Input"
|
| 28 |
-
outputs=gr.
|
| 29 |
-
title="
|
| 30 |
)
|
| 31 |
|
| 32 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# 1. Configuration - MAKE SURE THIS IS CORRECT
|
| 7 |
+
# Replace 'your-username/text-to-markdown-t5' with your actual repo name
|
| 8 |
+
model_id = "amielitos/text-to-markdown-t5"
|
| 9 |
+
|
| 10 |
+
print(f"--- Loading model: {model_id} ---")
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
tokenizer = T5Tokenizer.from_pretrained(model_id, legacy=False)
|
| 14 |
+
model = T5ForConditionalGeneration.from_pretrained(model_id)
|
| 15 |
+
print("--- Model and Tokenizer loaded successfully! ---")
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"--- ERROR LOADING MODEL: {str(e)} ---")
|
| 18 |
|
| 19 |
def convert_to_markdown(input_text):
|
| 20 |
+
print(f"--- Received input: {input_text[:50]}... ---")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
if not input_text.strip():
|
| 23 |
+
return "Please enter some text."
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# T5 prefix logic
|
| 27 |
+
full_input = f"markdown: {input_text}"
|
| 28 |
+
|
| 29 |
+
# Tokenize
|
| 30 |
+
inputs = tokenizer(full_input, return_tensors="pt", max_length=512, truncation=True)
|
| 31 |
+
print("--- Tokenization complete ---")
|
| 32 |
+
|
| 33 |
+
# Generate
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = model.generate(
|
| 36 |
+
inputs.input_ids,
|
| 37 |
+
max_length=512,
|
| 38 |
+
num_beams=2,
|
| 39 |
+
early_stopping=True
|
| 40 |
+
)
|
| 41 |
+
print("--- Generation complete ---")
|
| 42 |
+
|
| 43 |
+
# Decode
|
| 44 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
+
print(f"--- Decoded result: {result[:50]}... ---")
|
| 46 |
+
return result
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"--- ERROR DURING CONVERSION: {str(e)} ---")
|
| 50 |
+
return f"An error occurred: {str(e)}"
|
| 51 |
|
| 52 |
+
# Interface
|
| 53 |
demo = gr.Interface(
|
| 54 |
fn=convert_to_markdown,
|
| 55 |
+
inputs=gr.Textbox(lines=5, label="Plain Text Input"),
|
| 56 |
+
outputs=gr.Textbox(label="Result"), # Changed to Textbox temporarily for debugging
|
| 57 |
+
title="Text to Markdown Debugger"
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|