File size: 988 Bytes
094733b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
"""backend.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1ELiw5PgFIpVzc3HtrCiyILA9HbrL-WXg
"""

import fitz  # PyMuPDF
import os
from huggingface_hub import InferenceClient


client = InferenceClient(
    model="google/flan-t5-large",
    token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
)


def extract_text_from_pdf(pdf_file):
    text = ""
    doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
    for page in doc:
        text += page.get_text()
    return text


def generate_answer(user_question, pdf_context):
    prompt = f"""
You are a helpful assistant. Use the context from the PDF to answer the question clearly and accurately.

Context: {pdf_context}

Question: {user_question}

Answer:
"""
    response = client.text_generation(
        prompt=prompt,
        max_new_tokens=300,
        temperature=0.5,
        top_p=0.9,
        repetition_penalty=1.05
    )
    return response