Spaces:
Running
Running
| import gradio as gr | |
| from google import genai | |
| from google.genai import types | |
| import os | |
| # Funktion zur Verarbeitung des PDFs | |
| def extract_text_from_pdf(pdf_file): | |
| # 1. API Key aus den Hugging Face Secrets laden | |
| api_key = os.environ.get("GEMINI_API_KEY") | |
| if not api_key: | |
| return "Fehler: 'GOOGLE_API_KEY' wurde nicht in den Hugging Face Secrets gefunden." | |
| # 2. Client initialisieren | |
| client = genai.Client(api_key=api_key) | |
| try: | |
| # 3. Datei hochladen (file= Parameter verwenden) | |
| uploaded_file = client.files.upload(file=pdf_file) | |
| print(f"{uploaded_file=}") | |
| # 4. Inhalt generieren (Datei + Prompt) | |
| response = client.models.generate_content( | |
| #model='gemini-flash-latest', | |
| model='gemini-2.5-flash-lite', | |
| contents=[ | |
| uploaded_file, | |
| "\n\n", | |
| "Extract text from this PDF and return the original extracted text without any comments" | |
| ] | |
| ) | |
| # 5. Aufräumen: Datei bei Google löschen | |
| try: | |
| client.files.delete(name=uploaded_file.name) | |
| except: | |
| pass # Ignorieren, falls Löschen fehlschlägt | |
| return response.text | |
| except Exception as e: | |
| return f"Ein unerwarteter Fehler ist aufgetreten: {str(e)}" | |
| # Gradio Interface Definition | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 📄 PDF Text Extraktor (Gemini Flash)") | |
| gr.Markdown("Lade ein PDF hoch. Gemini 2.0 Flash extrahiert den Text basierend auf dem Prompt.") | |
| with gr.Row(): | |
| # Input: Datei (type='filepath' übergibt den Pfad zur temporären Datei) | |
| input_file = gr.File(label="PDF hier ablegen", file_types=[".pdf"], type="filepath") | |
| with gr.Row(): | |
| # Button zum Starten | |
| submit_btn = gr.Button("Text extrahieren", variant="primary") | |
| with gr.Row(): | |
| # Output: Textbox | |
| output_text = gr.Textbox(label="Extrahierter Text", lines=20, show_copy_button=True) | |
| # Event Listener | |
| submit_btn.click( | |
| fn=extract_text_from_pdf, | |
| inputs=input_file, | |
| outputs=output_text | |
| ) | |
| # App starten | |
| if __name__ == "__main__": | |
| demo.launch() |