vitellinho commited on
Commit
828dcd3
1 Parent(s): 59653f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -2,6 +2,7 @@ import spaces
2
  import gradio as gr
3
  from PyPDF2 import PdfReader
4
  from io import BytesIO
 
5
  from transformers import AutoTokenizer, AutoModelForCausalLM
6
  import torch
7
  import re
@@ -60,17 +61,34 @@ def generate_flashcards(text):
60
  except Exception as e:
61
  return f"Fehler bei der Generierung der Karteikarten: {str(e)}"
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  @spaces.GPU(duration=60)
64
  def process_pdf(pdf_file):
65
  if pdf_file is None:
66
- return []
67
 
68
  text = extract_text_from_pdf(pdf_file)
69
  if text.startswith("Fehler beim Lesen der PDF"):
70
- return []
71
 
72
  flashcards = generate_flashcards(text)
73
- return flashcards if isinstance(flashcards, list) else []
 
 
 
74
 
75
  def update_flashcard(flashcards, index, current_side):
76
  if not flashcards or index >= len(flashcards):
@@ -108,10 +126,12 @@ with gr.Blocks() as iface:
108
  question_box = gr.Textbox(label="Frage", interactive=False)
109
  answer_box = gr.Textbox(label="Antwort", interactive=False, visible=False)
110
 
 
 
111
  generate_button.click(
112
  process_pdf,
113
  inputs=[pdf_input],
114
- outputs=[flashcards_state]
115
  ).then(
116
  lambda cards: update_flashcard(cards, 0, "question") + (0, "question"),
117
  inputs=[flashcards_state],
 
2
  import gradio as gr
3
  from PyPDF2 import PdfReader
4
  from io import BytesIO
5
+ from fpdf import FPDF # Importiere FPDF, um PDF-Dateien zu erstellen
6
  from transformers import AutoTokenizer, AutoModelForCausalLM
7
  import torch
8
  import re
 
61
  except Exception as e:
62
  return f"Fehler bei der Generierung der Karteikarten: {str(e)}"
63
 
64
+ def create_pdf(flashcards):
65
+ pdf = FPDF()
66
+ pdf.add_page()
67
+ pdf.set_font("Arial", size=12)
68
+
69
+ for question, answer in flashcards:
70
+ pdf.multi_cell(0, 10, f"Frage: {question}")
71
+ pdf.multi_cell(0, 10, f"Antwort: {answer}")
72
+ pdf.ln(10) # Adds space between flashcards
73
+
74
+ pdf_file_path = "/mnt/data/flashcards_output.pdf"
75
+ pdf.output(pdf_file_path)
76
+ return pdf_file_path
77
+
78
  @spaces.GPU(duration=60)
79
  def process_pdf(pdf_file):
80
  if pdf_file is None:
81
+ return [], None
82
 
83
  text = extract_text_from_pdf(pdf_file)
84
  if text.startswith("Fehler beim Lesen der PDF"):
85
+ return [], None
86
 
87
  flashcards = generate_flashcards(text)
88
+ if isinstance(flashcards, list):
89
+ pdf_path = create_pdf(flashcards)
90
+ return flashcards, pdf_path
91
+ return [], None
92
 
93
  def update_flashcard(flashcards, index, current_side):
94
  if not flashcards or index >= len(flashcards):
 
126
  question_box = gr.Textbox(label="Frage", interactive=False)
127
  answer_box = gr.Textbox(label="Antwort", interactive=False, visible=False)
128
 
129
+ pdf_download_button = gr.File(label="Download Flashcards PDF")
130
+
131
  generate_button.click(
132
  process_pdf,
133
  inputs=[pdf_input],
134
+ outputs=[flashcards_state, pdf_download_button]
135
  ).then(
136
  lambda cards: update_flashcard(cards, 0, "question") + (0, "question"),
137
  inputs=[flashcards_state],