CoolFace
Apppublic

witcher23/pdf-extractor

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py98 linesDownload Raw Back to root
1import gradio as gr2from huggingface_hub import InferenceClient3import PyPDF24import io5 6 7"""8For more information n `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference9"""10client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")11 12 13def respond(14    message,15    history: list[tuple[str, str]],16    system_message,17    max_tokens,18    temperature,19    top_p,20):21    messages = [{"role": "system", "content": system_message}]22 23    for val in history:24        if val[0]:25            messages.append({"role": "user", "content": val[0]})26        if val[1]:27            messages.append({"role": "assistant", "content": val[1]})28 29    messages.append({"role": "user", "content": message})30 31    response = ""32 33    for message in client.chat_completion(34        messages,35        max_tokens=max_tokens,36        stream=True,37        temperature=temperature,38        top_p=top_p,39    ):40        token = message.choices[0].delta.content41 42        response += token43        yield response44 45 46def extract_text_from_pdf(pdf_file):47    if pdf_file is None:48        return "No file uploaded."49    50    try:51        pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file))52        text = ""53        for page in pdf_reader.pages:54            text += page.extract_text() + "\n\n"55        return text.strip()56    except Exception as e:57        return f"An error occurred: {str(e)}"58 59 60# Update the Chatbot component61 62 63"""64For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface65"""66# demo = gr.ChatInterface(67#     respond,68#     additional_inputs=[69#         gr.Textbox(value="You are a friendly Chatbot.", label="System message"),70#         gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),71#         gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),72#         gr.Slider(73#             minimum=0.1,74#             maximum=1.0,75#             value=0.95,76#             step=0.05,77#             label="Top-p (nucleus sampling)",78#         ),79#     ],80# )81 82pdf_interface = gr.Interface(83    fn=extract_text_from_pdf,84    inputs=gr.File(label="Upload PDF", type="binary"),85    outputs="text",86    title="PDF Text Extractor",87    description="Upload a PDF file to extract its text content."88)89 90# Create the tabbed interface91# demo = gr.TabbedInterface(92#     interface_list=[demo, pdf_interface],93#     tab_names=["Chat", "PDF Extractor"]94# )95 96if __name__ == "__main__":97    pdf_interface.launch()98