Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,37 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
client = InferenceClient("opennyaiorg/Aalap-Mistral-7B-v0.1-bf16")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
|
|
|
|
28 |
response = ""
|
|
|
29 |
|
30 |
for message in client.chat_completion(
|
31 |
messages,
|
@@ -35,29 +41,46 @@ def respond(
|
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
"""
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import fitz # PyMuPDF
|
4 |
|
|
|
|
|
|
|
5 |
client = InferenceClient("opennyaiorg/Aalap-Mistral-7B-v0.1-bf16")
|
6 |
|
7 |
+
def extract_text_from_pdf(pdf_file):
|
8 |
+
document = fitz.open(pdf_file.name)
|
9 |
+
text = ""
|
10 |
+
for page_num in range(len(document)):
|
11 |
+
page = document.load_page(page_num)
|
12 |
+
text += page.get_text()
|
13 |
+
return text
|
14 |
|
15 |
+
def summarize_pdf(pdf_file, max_tokens, temperature, top_p):
|
16 |
+
text = extract_text_from_pdf(pdf_file)
|
17 |
+
response = ""
|
18 |
+
messages = [{"role": "user", "content": f"Summarize the following text: {text}"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
for message in client.chat_completion(
|
21 |
+
messages,
|
22 |
+
max_tokens=max_tokens,
|
23 |
+
stream=True,
|
24 |
+
temperature=temperature,
|
25 |
+
top_p=top_p,
|
26 |
+
):
|
27 |
+
token = message.choices[0].delta.content
|
28 |
+
response += token
|
29 |
+
yield response
|
30 |
|
31 |
+
def ner_pdf(pdf_file, max_tokens, temperature, top_p):
|
32 |
+
text = extract_text_from_pdf(pdf_file)
|
33 |
response = ""
|
34 |
+
messages = [{"role": "user", "content": f"Extract named entities from the following text: {text}"}]
|
35 |
|
36 |
for message in client.chat_completion(
|
37 |
messages,
|
|
|
41 |
top_p=top_p,
|
42 |
):
|
43 |
token = message.choices[0].delta.content
|
44 |
+
response += token
|
45 |
+
yield response
|
46 |
+
|
47 |
+
def qa_pdf(pdf_file, question, max_tokens, temperature, top_p):
|
48 |
+
text = extract_text_from_pdf(pdf_file)
|
49 |
+
response = ""
|
50 |
+
messages = [{"role": "user", "content": f"Answer the question '{question}' based on the following text: {text}"}]
|
51 |
|
52 |
+
for message in client.chat_completion(
|
53 |
+
messages,
|
54 |
+
max_tokens=max_tokens,
|
55 |
+
stream=True,
|
56 |
+
temperature=temperature,
|
57 |
+
top_p=top_p,
|
58 |
+
):
|
59 |
+
token = message.choices[0].delta.content
|
60 |
response += token
|
61 |
yield response
|
62 |
|
63 |
+
with gr.Blocks() as demo:
|
64 |
+
gr.Markdown("# NLP Tasks on PDF Documents")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
with gr.Tab("Summarization"):
|
67 |
+
pdf_file = gr.File(label="Upload PDF")
|
68 |
+
summarize_button = gr.Button("Summarize")
|
69 |
+
summary_output = gr.Textbox(label="Summary")
|
70 |
+
summarize_button.click(summarize_pdf, inputs=[pdf_file, gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")], outputs=summary_output)
|
71 |
+
|
72 |
+
with gr.Tab("Named Entity Recognition (NER)"):
|
73 |
+
pdf_file = gr.File(label="Upload PDF")
|
74 |
+
ner_button = gr.Button("Extract Entities")
|
75 |
+
ner_output = gr.JSON(label="Entities")
|
76 |
+
ner_button.click(ner_pdf, inputs=[pdf_file, gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")], outputs=ner_output)
|
77 |
+
|
78 |
+
with gr.Tab("Question Answering"):
|
79 |
+
pdf_file = gr.File(label="Upload PDF")
|
80 |
+
question_input = gr.Textbox(label="Enter your question")
|
81 |
+
qa_button = gr.Button("Get Answer")
|
82 |
+
qa_output = gr.Textbox(label="Answer")
|
83 |
+
qa_button.click(qa_pdf, inputs=[pdf_file, question_input, gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")], outputs=qa_output)
|
84 |
|
85 |
if __name__ == "__main__":
|
86 |
+
demo.launch()
|