Update app.py
Browse files
app.py
CHANGED
@@ -29,36 +29,36 @@ def summarize(text):
|
|
29 |
|
30 |
return summary
|
31 |
|
32 |
-
# API_URL = "https://api-inference.huggingface.co/models/deepset/bert-large-uncased-whole-word-masking-squad2"
|
33 |
-
# headers = {"Authorization": f"Bearer {my_key}"}
|
34 |
-
|
35 |
-
# def query(payload):
|
36 |
-
# response = requests.post(API_URL, headers=headers, json=payload)
|
37 |
-
# return response.content
|
38 |
-
|
39 |
def answer_question(text, question):
|
40 |
response = qa_model(question=question, context=text)
|
41 |
answer = response['answer']
|
42 |
return answer
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
def summarize_and_qa(pdf_file, question):
|
45 |
text = extract_text_from_pdf(pdf_file)
|
46 |
summary = summarize(text)
|
47 |
answer = answer_question(text, question)
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
57 |
|
58 |
gr.Interface(
|
59 |
fn=summarize_and_qa,
|
60 |
inputs=["file", "text"],
|
61 |
-
outputs=["textbox", "textbox"],
|
62 |
title="Understand your PDF Better",
|
63 |
-
description="Upload a PDF to get a summary. You can ask any question regardging the content of the PDF. It will also generate a picture to
|
64 |
).launch(debug=True, share=True)
|
|
|
29 |
|
30 |
return summary
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
def answer_question(text, question):
|
33 |
response = qa_model(question=question, context=text)
|
34 |
answer = response['answer']
|
35 |
return answer
|
36 |
|
37 |
+
def query(payload):
|
38 |
+
API_URL = "https://api-inference.huggingface.co/models/stable-diffusion-v1-5/stable-diffusion-v1-5"
|
39 |
+
headers = {"Authorization": f"Bearer {my_key}"}
|
40 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
41 |
+
return response.content
|
42 |
+
|
43 |
def summarize_and_qa(pdf_file, question):
|
44 |
text = extract_text_from_pdf(pdf_file)
|
45 |
summary = summarize(text)
|
46 |
answer = answer_question(text, question)
|
47 |
+
image_bytes = query({"inputs": summary})
|
48 |
+
if image_bytes:
|
49 |
+
try:
|
50 |
+
image = Image.open(io.BytesIO(image_bytes))
|
51 |
+
except Exception as e:
|
52 |
+
return summary, answer, None
|
53 |
+
else:
|
54 |
+
image = None
|
55 |
+
|
56 |
+
return summary, answer, image
|
57 |
|
58 |
gr.Interface(
|
59 |
fn=summarize_and_qa,
|
60 |
inputs=["file", "text"],
|
61 |
+
outputs=["textbox", "textbox", "image"],
|
62 |
title="Understand your PDF Better",
|
63 |
+
description="Upload a PDF to get a summary. You can ask any question regardging the content of the PDF. It will also generate a picture related to your PDF."
|
64 |
).launch(debug=True, share=True)
|