Haonan Liu
commited on
Commit
·
2e7eced
1
Parent(s):
f851d13
Add application file
Browse files- app.py +80 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from GPTagger import *
|
| 4 |
+
from langchain.prompts import PromptTemplate
|
| 5 |
+
|
| 6 |
+
default_prompt = """
|
| 7 |
+
Please understand the instructions above and do extraction in the text below.
|
| 8 |
+
|
| 9 |
+
TEXT:
|
| 10 |
+
\"\"\"
|
| 11 |
+
{text}
|
| 12 |
+
\"\"\"
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
def ner(
|
| 16 |
+
model: str,
|
| 17 |
+
nr_call: int,
|
| 18 |
+
tag_name: str,
|
| 19 |
+
tag_max_len: int,
|
| 20 |
+
text: str,
|
| 21 |
+
prompt: str,
|
| 22 |
+
):
|
| 23 |
+
cfg = NerConfig(
|
| 24 |
+
tag_name=tag_name,
|
| 25 |
+
model=model,
|
| 26 |
+
nr_calls=nr_call,
|
| 27 |
+
tag_max_len=tag_max_len,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
ner_pipeline = NerPipeline.from_config(cfg)
|
| 31 |
+
template = PromptTemplate.from_template(prompt)
|
| 32 |
+
|
| 33 |
+
extractions = ner_pipeline(text, template, "")
|
| 34 |
+
|
| 35 |
+
if not extractions:
|
| 36 |
+
output = []
|
| 37 |
+
else:
|
| 38 |
+
output = [
|
| 39 |
+
{"entity": tag_name.upper(), "start": item.start, "end": item.end}
|
| 40 |
+
for item in extractions
|
| 41 |
+
]
|
| 42 |
+
return {"text": text, "entities": output}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
with gr.Blocks(theme=gr.themes.Default(text_size=gr.themes.sizes.text_lg)) as demo:
|
| 46 |
+
with gr.Row():
|
| 47 |
+
tag_name = gr.Textbox(label="tag name")
|
| 48 |
+
tag_max_len = gr.Slider(
|
| 49 |
+
minimum=10, maximum=1000, step=10, label="max length of the tag"
|
| 50 |
+
)
|
| 51 |
+
with gr.Row():
|
| 52 |
+
model = gr.Dropdown(
|
| 53 |
+
["gpt-3.5-turbo-0613", "gpt-4-0613"],
|
| 54 |
+
label="model_name",
|
| 55 |
+
value="gpt-3.5-turbo-0613",
|
| 56 |
+
)
|
| 57 |
+
nr_call = gr.Number(label="nr_of_calls", minimum=1, value=1, precision=0)
|
| 58 |
+
with gr.Row():
|
| 59 |
+
prompt = gr.TextArea(
|
| 60 |
+
placeholder="Enter your prompt here...",
|
| 61 |
+
label="prompt",
|
| 62 |
+
value=default_prompt,
|
| 63 |
+
)
|
| 64 |
+
text = gr.TextArea(placeholder="Enter your text here...", label="text")
|
| 65 |
+
btn = gr.Button("Submit")
|
| 66 |
+
output = gr.HighlightedText()
|
| 67 |
+
btn.click(
|
| 68 |
+
ner,
|
| 69 |
+
inputs=[
|
| 70 |
+
model,
|
| 71 |
+
nr_call,
|
| 72 |
+
tag_name,
|
| 73 |
+
tag_max_len,
|
| 74 |
+
text,
|
| 75 |
+
prompt,
|
| 76 |
+
],
|
| 77 |
+
outputs=output,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gptagger
|