Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +2 -8
- main.py +92 -0
- requirements.txt +1 -0
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: green
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.15.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: gradio_web
|
3 |
+
app_file: main.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
sdk_version: 4.15.0
|
|
|
|
|
6 |
---
|
|
|
|
main.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import re
|
4 |
+
import string
|
5 |
+
import random
|
6 |
+
import argparse
|
7 |
+
from bs4 import BeautifulSoup
|
8 |
+
|
9 |
+
contents = ''
|
10 |
+
|
11 |
+
def make_body(file):
|
12 |
+
with open(file.name, 'r') as xml_file:
|
13 |
+
text = xml_file.read()
|
14 |
+
xml = BeautifulSoup(text, "xml")
|
15 |
+
global contents
|
16 |
+
contents = str(xml.find_all('body')).strip('[]')
|
17 |
+
allReferences = []
|
18 |
+
|
19 |
+
for tag in xml.findAll('ref'):
|
20 |
+
allReferences.append(tag.get('id'))
|
21 |
+
|
22 |
+
with open("body.xml", 'w') as f:
|
23 |
+
|
24 |
+
f.write(make_refs(contents, allReferences))
|
25 |
+
return "body.xml"
|
26 |
+
|
27 |
+
def index_to_ref_id(reference_text, reference_ids):
|
28 |
+
reference_text = reference_text.strip('[]')
|
29 |
+
dash_separator = "–"
|
30 |
+
comma_separator = ','
|
31 |
+
id_max = len(reference_ids)
|
32 |
+
ids = []
|
33 |
+
refs = []
|
34 |
+
|
35 |
+
if reference_text.find(dash_separator) != -1:
|
36 |
+
s, e = [int(x) for x in reference_text.split(dash_separator)]
|
37 |
+
for i in range(s,e+1):
|
38 |
+
ids.append(i)
|
39 |
+
elif reference_text.find(comma_separator) != -1:
|
40 |
+
ids = [int(x) for x in reference_text.split(comma_separator)]
|
41 |
+
else:
|
42 |
+
ids.append(int(reference_text))
|
43 |
+
|
44 |
+
for id in ids:
|
45 |
+
refs.append(reference_ids[id-1])
|
46 |
+
|
47 |
+
return refs
|
48 |
+
|
49 |
+
# https://www.geeksforgeeks.org/python-generate-random-string-of-given-length/
|
50 |
+
def generate_id(type, length=32):
|
51 |
+
N = length
|
52 |
+
|
53 |
+
# using random.choices()
|
54 |
+
# generating random strings
|
55 |
+
res = type + '-' + ''.join(random.choices(string.ascii_lowercase +
|
56 |
+
string.digits, k=N))
|
57 |
+
|
58 |
+
return(str(res))
|
59 |
+
|
60 |
+
|
61 |
+
def xref_generator(reference_text, reference_ids):
|
62 |
+
id_type = "xref"
|
63 |
+
references_str = ' '.join(index_to_ref_id(reference_text, reference_ids))
|
64 |
+
generated_xref = '<xref id="' + generate_id(id_type) + '" ref-type="bibr" rid="' + references_str + '">' + reference_text + '</xref>'
|
65 |
+
|
66 |
+
return generated_xref
|
67 |
+
|
68 |
+
def make_refs(article_body, reference_ids):
|
69 |
+
citations_set = set()
|
70 |
+
|
71 |
+
citations_raw = re.findall(r'(\[\d+–\d+\]|\[\d+(, \d+)*\])', contents)
|
72 |
+
|
73 |
+
for cit in citations_raw:
|
74 |
+
citations_set.add(cit[0])
|
75 |
+
|
76 |
+
x = contents
|
77 |
+
|
78 |
+
for cit in citations_set:
|
79 |
+
x = x.replace(str(cit), str(xref_generator(cit, reference_ids)))
|
80 |
+
|
81 |
+
return x
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
demo = gr.Interface(
|
86 |
+
make_body,
|
87 |
+
gr.File(file_count="single", file_types=[".xml"]),
|
88 |
+
"file",
|
89 |
+
allow_flagging="never"
|
90 |
+
)
|
91 |
+
|
92 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
beautifulsoup4
|