amielitos commited on
Commit
92d0083
·
verified ·
1 Parent(s): 9f55a18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -19
app.py CHANGED
@@ -1,32 +1,60 @@
1
  import gradio as gr
2
  import torch
3
  from transformers import T5ForConditionalGeneration, T5Tokenizer
 
4
 
5
- # 1. Load the model and tokenizer explicitly
6
- model_id = "amielitos/text-to-markdown-t5" # Replace with your actual username/model
7
- tokenizer = T5Tokenizer.from_pretrained(model_id)
8
- model = T5ForConditionalGeneration.from_pretrained(model_id)
 
 
 
 
 
 
 
 
9
 
10
  def convert_to_markdown(input_text):
11
- # 2. T5 requires the prefix we used in training
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
- # 4. Generate output
18
- with torch.no_grad():
19
- outputs = model.generate(inputs.input_ids, max_length=512)
20
-
21
- # 5. Decode back to text
22
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # 6. Create the Interface
25
  demo = gr.Interface(
26
  fn=convert_to_markdown,
27
- inputs=gr.Textbox(lines=5, label="Plain Text Input", placeholder="Type your text here..."),
28
- outputs=gr.Markdown(label="Markdown Result"),
29
- title="AI Text to Markdown Converter"
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__":