Spaces:
Sleeping
Sleeping
Shunfeng Zheng commited on
Update 1_SpatialParse.py
Browse files10 unmodified lines111213141516171819202122232425262728293031323334353637383940414243186 unmodified lines23023123223323423523623723823924024124224324424524624710 unmodified linesfrom utils import geoutilimport urllib.parseimport os# import requestsimport pydanticst.write("Pydantic version:", pydantic.__version__)# API_TOKEN = 'hf_'# BACKEND_URL = "https://dsbb0707-dockerb2.hf.space/api/predict/"# def call_backend(input_text):# try:# headers = {# "Authorization": f"Bearer {API_TOKEN}"# }# response = requests.post(# BACKEND_URL,# headers=headers,# json={"data": [input_text]},# timeout=10# )# if response.status_code == 200:# result = response.json()["data"][0]# return f"✅ {result['result']}\n⏰ {result['timestamp']}"# return f"❌ Backend Error (HTTP {response.status_code})"# except Exception as e:# return f"⚠️ Connection Error: {str(e)}"colors = {'GPE': "#43c6fc", "LOC": "#fd9720", "RSE":"#a6e22d"}186 unmodified lines# # doc.to_disk("saved_doc.spacy")# doc.to_disk("/tmp/saved_doc.spacy")# api_result = call_backend(text)# st.markdown(api_result)# st.text_area(api_result)# html = displacy.render(doc,style="ent", options = options)# html = html.replace("\n","")# st.write(HTML_WRAPPER.format(html),unsafe_allow_html=True)# show_spatial_ent_table(doc, text)# st.markdown("123123")# show_sentence_selector_table(doc_copy)passdef show_sentence_selector_table(doc_copy):st.markdown("**______________________________________________________________________________________**")10 unmodified lines111213141516171819202122232425262728293031323334353637383940414243444546186 unmodified lines23323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527610 unmodified linesfrom utils import geoutilimport urllib.parseimport osimport requestsfrom spacy.tokens import Docfrom spacy.lang.en import Englishimport pydanticprint("Pydantic version:", pydantic.__version__)API_TOKEN = os.getenv('API_TOKEN1')BACKEND_URL = "https://dsbb0707-dockerb2.hf.space/api/predict/"def call_backend(input_text):try:headers = {"Authorization": f"Bearer {API_TOKEN}"}response = requests.post(BACKEND_URL,headers=headers,json={"data": [input_text]},timeout=10)if response.status_code == 200:return response.json() # ✅ 保留原始 JSON 对象 (dict)return {"error": f"❌ Backend Error (HTTP {response.status_code})"}except Exception as e:return {"error": f"⚠️ Connection Error: {str(e)}"}colors = {'GPE': "#43c6fc", "LOC": "#fd9720", "RSE":"#a6e22d"}186 unmodified lines# # doc.to_disk("saved_doc.spacy")# doc.to_disk("/tmp/saved_doc.spacy")Span.set_extension("rse_id", default="", force=True)api_result = call_backend(text)print(api_result, 'dadada')doc_element = api_result["data"][0]st.markdown(type(api_result))st.markdown(doc_element)# doc_element = {'text': 'Between Burwood and Glebe.', 'ents': [{'start': 8, 'end': 15, 'label': 'GPE'}, {'start': 20, 'end': 25, 'label': 'GPE'}], 'tokens': [{'id': 0, 'start': 0, 'end': 7}, {'id': 1, 'start': 8, 'end': 15}, {'id': 2, 'start': 16, 'end': 19}, {'id': 3, 'start': 20, 'end': 25}, {'id': 4, 'start': 25, 'end': 26}], 'ents_ext': [{'start': 8, 'end': 15, 'label': 'GPE', 'rse_id': 'Burwood'}, {'start': 20, 'end': 25, 'label': 'GPE', 'rse_id': 'Glebe'}]}# doc_element = {'text': 'I would like to know where is the area between Burwood and Glebe. Pyrmont.', 'ents': [{'start': 47, 'end': 54, 'label': 'GPE'}, {'start': 59, 'end': 64, 'label': 'GPE'}, {'start': 66, 'end': 73, 'label': 'GPE'}], 'sents': [{'start': 0, 'end': 65}, {'start': 66, 'end': 74}], 'tokens': [{'id': 0, 'start': 0, 'end': 1}, {'id': 1, 'start': 2, 'end': 7}, {'id': 2, 'start': 8, 'end': 12}, {'id': 3, 'start': 13, 'end': 15}, {'id': 4, 'start': 16, 'end': 20}, {'id': 5, 'start': 21, 'end': 26}, {'id': 6, 'start': 27, 'end': 29}, {'id': 7, 'start': 30, 'end': 33}, {'id': 8, 'start': 34, 'end': 38}, {'id': 9, 'start': 39, 'end': 46}, {'id': 10, 'start': 47, 'end': 54}, {'id': 11, 'start': 55, 'end': 58}, {'id': 12, 'start': 59, 'end': 64}, {'id': 13, 'start': 64, 'end': 65}, {'id': 14, 'start': 66, 'end': 73}, {'id': 15, 'start': 73, 'end': 74}]}# doc_element =nlp = English()nlp.add_pipe("sentencizer")doc = Doc(nlp.vocab).from_json(doc_element)doc = nlp.get_pipe("sentencizer")(doc)st.markdown(type(doc))for ent_ext in doc_element["ents_ext"]:for ent in doc.ents:if ent.start_char == ent_ext["start"] and ent.end_char == ent_ext["end"]:ent._.rse_id = ent_ext["rse_id"]doc = set_selected_entities(doc)doc.to_disk("saved_doc.spacy")doc.to_disk("/tmp/saved_doc.spacy")html = displacy.render(doc,style="ent", options = options)html = html.replace("\n","")st.write(HTML_WRAPPER.format(html),unsafe_allow_html=True)show_spatial_ent_table(doc, text)st.markdown("123123")show_sentence_selector_table(doc)passdef show_sentence_selector_table(doc_copy):st.markdown("**______________________________________________________________________________________**")
1_SpatialParse.py+59-30