CoolFace
Apppublic

mathiaseggert/WIPIFHAC

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py61 linesDownload Raw Back to root
1import gradio as gr2from huggingface_hub import InferenceClient3import PyPDF24 5# --- PDF laden und Text extrahieren ---6pdf_path = "pruefungsordnung.pdf"  # vorher ins Space hochladen!7pdf_text = ""8with open(pdf_path, "rb") as f:9    reader = PyPDF2.PdfReader(f)10    for page in reader.pages:11        text = page.extract_text()12        if text:13            pdf_text += text + "\n"14 15# --- PDF in Abschnitte splitten ---16section_size = 50017pdf_sections = [pdf_text[i:i+section_size] for i in range(0, len(pdf_text), section_size)]18 19# --- InferenceClient initialisieren ---20client = InferenceClient()21 22# --- Antwortfunktion mit semantischer Suche ---23def respond(message, history, system_message, max_tokens, temperature, top_p):24    # Frage-Antwort-Aufruf an Hugging Face Inference API25    answers = []26    for section in pdf_sections:27        response = client.question_answering(28            question=message,29            context=section30        )31        answers.append((response.answer, response.score))32 33    # Beste Antwort basierend auf der höchsten Score auswählen34    best_answer = max(answers, key=lambda x: x[1], default=("Keine Antwort gefunden", 0))35    answer, score = best_answer36 37    if score > 0.1:38        snippet = answer39    else:40        snippet = "Ich habe dazu leider keine passende Stelle in der Prüfungsordnung gefunden."41 42    return f"{system_message}\n\n{snippet}"43 44# --- Chatbot-Interface ---45chatbot = gr.ChatInterface(46    respond,47    type="messages",48    additional_inputs=[49        gr.Textbox(50            value="Du bist Studiengangberater für den Studiengang Wirtschaftsinformatik an der FH Aachen und berätst Studierende. Greife dazu auf die Prüfungsordnung zu.",51            label="System message"52        ),53        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),54        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),55        gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),56    ],57)58 59# Start60if __name__ == "__main__":61    chatbot.launch()