Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,92 @@
|
|
1 |
import subprocess
|
2 |
subprocess.run(["pip", "uninstall", "gradio"])
|
3 |
-
subprocess.run(["pip", "install", "utca>=0.1.0"])
|
4 |
subprocess.run(["pip", "install", "gradio==4.31.5"])
|
5 |
|
6 |
import gradio as gr
|
7 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
13 |
|
14 |
-
|
15 |
-
|
|
|
1 |
import subprocess
|
2 |
subprocess.run(["pip", "uninstall", "gradio"])
|
|
|
3 |
subprocess.run(["pip", "install", "gradio==4.31.5"])
|
4 |
|
5 |
import gradio as gr
|
6 |
+
from typing import Dict, Union
|
7 |
+
from gliner import GLiNER
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
model = GLiNER.from_pretrained("knowledgator/gliner-multitask-v1.0").to("cpu")
|
11 |
+
|
12 |
+
text2 = """
|
13 |
+
Apple Inc. is an American multinational technology company headquartered in Cupertino, California. Apple is the world's largest technology company by revenue, with US$394.3 billion in 2022 revenue. As of March 2023, Apple is the world's biggest company by market capitalization. As of June 2022, Apple is the fourth-largest personal computer vendor by unit sales and the second-largest mobile phone manufacturer in the world. It is considered one of the Big Five American information technology companies, alongside Alphabet (parent company of Google), Amazon, Meta Platforms, and Microsoft.
|
14 |
+
Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975 to develop and sell BASIC interpreters for the Altair 8800. During his career at Microsoft, Gates held the positions of chairman, chief executive officer, president and chief software architect, while also being the largest individual shareholder until May 2014.
|
15 |
+
Apple was founded as Apple Computer Company on April 1, 1976, by Steve Wozniak, Steve Jobs (1955–2011) and Ronald Wayne to develop and sell Wozniak's Apple I personal computer. It was incorporated by Jobs and Wozniak as Apple Computer, Inc. in 1977. The company's second computer, the Apple II, became a best seller and one of the first mass-produced microcomputers. Apple went public in 1980 to instant financial success. The company developed computers featuring innovative graphical user interfaces, including the 1984 original Macintosh, announced that year in a critically acclaimed advertisement called "1984". By 1985, the high cost of its products, and power struggles between executives, caused problems. Wozniak stepped back from Apple and pursued other ventures, while Jobs resigned and founded NeXT, taking some Apple employees with him.
|
16 |
+
"""
|
17 |
+
|
18 |
+
qa_examples = [
|
19 |
+
[
|
20 |
+
f"Who are the founders of Microsoft:",
|
21 |
+
text2,
|
22 |
+
0.1,
|
23 |
+
False
|
24 |
+
],
|
25 |
+
[
|
26 |
+
f"Who are the founders of Apple?:",
|
27 |
+
text2,
|
28 |
+
0.5,
|
29 |
+
False
|
30 |
+
]]
|
31 |
+
|
32 |
+
def merge_entities(entities):
|
33 |
+
if not entities:
|
34 |
+
return []
|
35 |
+
merged = []
|
36 |
+
current = entities[0]
|
37 |
+
for next_entity in entities[1:]:
|
38 |
+
if next_entity['entity'] == current['entity'] and (next_entity['start'] == current['end'] + 1 or next_entity['start'] == current['end']):
|
39 |
+
current['word'] += ' ' + next_entity['word']
|
40 |
+
current['end'] = next_entity['end']
|
41 |
+
else:
|
42 |
+
merged.append(current)
|
43 |
+
current = next_entity
|
44 |
+
merged.append(current)
|
45 |
+
return merged
|
46 |
+
|
47 |
+
def process(
|
48 |
+
question:str, text, threshold: float, nested_ner: bool, labels: str = ["answer"]
|
49 |
+
) -> Dict[str, Union[str, int, float]]:
|
50 |
+
text = question + "\n" + text
|
51 |
+
r = {
|
52 |
+
"text": text,
|
53 |
+
"entities": [
|
54 |
+
{
|
55 |
+
"entity": entity["label"],
|
56 |
+
"word": entity["text"],
|
57 |
+
"start": entity["start"],
|
58 |
+
"end": entity["end"],
|
59 |
+
"score": 0,
|
60 |
+
}
|
61 |
+
for entity in model.predict_entities(
|
62 |
+
text, labels, flat_ner=not nested_ner, threshold=threshold
|
63 |
+
)
|
64 |
+
],
|
65 |
+
}
|
66 |
+
r["entities"] = merge_entities(r["entities"])
|
67 |
+
return r
|
68 |
+
|
69 |
+
with gr.Blocks(title="Question Answering Task") as qa_interface:
|
70 |
+
question = gr.Textbox(label="Question", placeholder="Enter your question here")
|
71 |
+
input_text = gr.Textbox(label="Text input", placeholder="Enter your text here")
|
72 |
+
threshold = gr.Slider(0, 1, value=0.3, step=0.01, label="Threshold", info="Lower the threshold to increase how many entities get predicted.")
|
73 |
+
nested_ner = gr.Checkbox(label="Nested NER", info="Allow for nested NER?")
|
74 |
+
output = gr.HighlightedText(label="Predicted Entities")
|
75 |
+
submit_btn = gr.Button("Submit")
|
76 |
+
examples = gr.Examples(
|
77 |
+
qa_examples,
|
78 |
+
fn=process,
|
79 |
+
inputs=[question, input_text, threshold, nested_ner],
|
80 |
+
outputs=output,
|
81 |
+
cache_examples=True
|
82 |
+
)
|
83 |
+
theme=gr.themes.Base()
|
84 |
|
85 |
+
input_text.submit(fn=process, inputs=[question, input_text, threshold, nested_ner], outputs=output)
|
86 |
+
question.submit(fn=process, inputs=[question, input_text, threshold, nested_ner], outputs=output)
|
87 |
+
threshold.release(fn=process, inputs=[question, input_text, threshold, nested_ner], outputs=output)
|
88 |
+
submit_btn.click(fn=process, inputs=[question, input_text, threshold, nested_ner], outputs=output)
|
89 |
+
nested_ner.change(fn=process, inputs=[question, input_text, threshold, nested_ner], outputs=output)
|
90 |
|
91 |
+
qa_interface.queue()
|
92 |
+
qa_interface.launch(debug=True, share=True)
|