Upload backend.py
Browse files- backend.py +46 -0
backend.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""backend.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1ELiw5PgFIpVzc3HtrCiyILA9HbrL-WXg
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import fitz # PyMuPDF
|
| 11 |
+
import os
|
| 12 |
+
from huggingface_hub import InferenceClient
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
client = InferenceClient(
|
| 16 |
+
model="google/flan-t5-large",
|
| 17 |
+
token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def extract_text_from_pdf(pdf_file):
|
| 22 |
+
text = ""
|
| 23 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 24 |
+
for page in doc:
|
| 25 |
+
text += page.get_text()
|
| 26 |
+
return text
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def generate_answer(user_question, pdf_context):
|
| 30 |
+
prompt = f"""
|
| 31 |
+
You are a helpful assistant. Use the context from the PDF to answer the question clearly and accurately.
|
| 32 |
+
|
| 33 |
+
Context: {pdf_context}
|
| 34 |
+
|
| 35 |
+
Question: {user_question}
|
| 36 |
+
|
| 37 |
+
Answer:
|
| 38 |
+
"""
|
| 39 |
+
response = client.text_generation(
|
| 40 |
+
prompt=prompt,
|
| 41 |
+
max_new_tokens=300,
|
| 42 |
+
temperature=0.5,
|
| 43 |
+
top_p=0.9,
|
| 44 |
+
repetition_penalty=1.05
|
| 45 |
+
)
|
| 46 |
+
return response
|