Spaces:
Runtime error
Runtime error
Commit
·
d19b3e0
1
Parent(s):
a4df109
init
Browse files- README.md +12 -0
- app.py +119 -0
- requirements.txt +2 -0
README.md
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Name Entity Recognition (Group 30)
|
3 |
+
emoji: 📹
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
6 |
+
sdk: streamlit
|
7 |
+
app_file: app.py
|
8 |
+
pinned: false
|
9 |
+
---
|
10 |
+
|
11 |
+
# image2textapp
|
12 |
+
Group 30 Demo - Name Entity Recognition
|
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import csv
|
4 |
+
import datetime
|
5 |
+
import time
|
6 |
+
import pandas as pd
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
|
9 |
+
##########################
|
10 |
+
headers = {"Authorization": "Bearer "}
|
11 |
+
API_URL = "https://api-inference.huggingface.co/models/cccmatthew/surrey-gp30"
|
12 |
+
##########################
|
13 |
+
|
14 |
+
|
15 |
+
def load_response_times():
|
16 |
+
try:
|
17 |
+
df = pd.read_csv('model_interactions.csv', usecols=["Timestamp", "Response Time"])
|
18 |
+
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
|
19 |
+
return df
|
20 |
+
except Exception as e:
|
21 |
+
st.error(f"Failed to read response times: {e}")
|
22 |
+
return pd.DataFrame()
|
23 |
+
|
24 |
+
def plot_response_times(df):
|
25 |
+
if not df.empty:
|
26 |
+
plt.figure(figsize=(10, 5))
|
27 |
+
plt.plot(df['Timestamp'], df['Response Time'], marker='o', linestyle='-')
|
28 |
+
plt.title('Response Times Over Time')
|
29 |
+
plt.xlabel('Timestamp')
|
30 |
+
plt.ylabel('Response Time (seconds)')
|
31 |
+
plt.grid(True)
|
32 |
+
st.pyplot(plt)
|
33 |
+
else:
|
34 |
+
st.write("No response time data to display.")
|
35 |
+
|
36 |
+
#Function to setup the logs ina csv file
|
37 |
+
def setup_csv_logger():
|
38 |
+
with open('model_interactions.csv', 'a', newline='') as file:
|
39 |
+
writer = csv.writer(file)
|
40 |
+
#The headers will be written if not present
|
41 |
+
if file.tell() == 0:
|
42 |
+
writer.writerow(["Timestamp", "User Input", "Model Prediction", "Response Time"])
|
43 |
+
|
44 |
+
def log_to_csv(sentence, results, response_time):
|
45 |
+
with open('model_interactions.csv', 'a', newline='') as file:
|
46 |
+
writer = csv.writer(file)
|
47 |
+
writer.writerow([datetime.datetime.now(), sentence, results, response_time])
|
48 |
+
|
49 |
+
setup_csv_logger()
|
50 |
+
|
51 |
+
st.title('Group 30 - DistilBERT')
|
52 |
+
st.write('This application uses DistilBERT to classify Abbreviations (AC) and Long Forms (LF)')
|
53 |
+
|
54 |
+
example_sentences = [
|
55 |
+
"RAFs are plotted for a selection of neurons in the dorsal zone (DZ) of auditory cortex in Figure 1.",
|
56 |
+
"Light dissolved inorganic carbon (DIC) resulting from the oxidation of hydrocarbons.",
|
57 |
+
"Images were acquired using a GE 3.0T MRI scanner with an upgrade for echo-planar imaging (EPI)."
|
58 |
+
]
|
59 |
+
|
60 |
+
sentence = st.selectbox('Choose an example sentence or type your own below:', example_sentences + ['Custom Input...'])
|
61 |
+
|
62 |
+
if sentence == 'Custom Input...':
|
63 |
+
sentence = st.text_input('Input your sentence here:', '')
|
64 |
+
|
65 |
+
def merge_entities(sentence, entities):
|
66 |
+
entities = sorted(entities, key=lambda x: x['start'])
|
67 |
+
annotated_sentence = ""
|
68 |
+
last_end = 0
|
69 |
+
for entity in entities:
|
70 |
+
annotated_sentence += sentence[last_end:entity['start']]
|
71 |
+
annotated_sentence += f"<mark style='background-color: #ffcccb;'><b>{sentence[entity['start']:entity['end']]}</b> [{entity['entity_group']}]</mark>"
|
72 |
+
last_end = entity['end']
|
73 |
+
annotated_sentence += sentence[last_end:]
|
74 |
+
return annotated_sentence
|
75 |
+
|
76 |
+
def send_request_with_retry(url, headers, json_data, retries=3, backoff_factor=1):
|
77 |
+
"""Send request with retries on timeouts and HTTP 503 errors."""
|
78 |
+
for attempt in range(retries):
|
79 |
+
start_time = time.time()
|
80 |
+
try:
|
81 |
+
response = requests.post(url, headers=headers, json=json_data)
|
82 |
+
response.raise_for_status()
|
83 |
+
response_time = time.time() - start_time
|
84 |
+
return response, response_time
|
85 |
+
except requests.exceptions.HTTPError as e:
|
86 |
+
if response.status_code == 503:
|
87 |
+
st.info('Server is unavailable, retrying...')
|
88 |
+
else:
|
89 |
+
raise
|
90 |
+
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
|
91 |
+
st.info(f"Network issue ({str(e)}), retrying...")
|
92 |
+
time.sleep(backoff_factor * (2 ** attempt))
|
93 |
+
|
94 |
+
st.error("Failed to process request after several attempts.")
|
95 |
+
return None, None
|
96 |
+
|
97 |
+
if st.button('Classify'):
|
98 |
+
if sentence:
|
99 |
+
API_URL = API_URL
|
100 |
+
headers = headers
|
101 |
+
response, response_time = send_request_with_retry(API_URL, headers, {"inputs": sentence})
|
102 |
+
if response is not None:
|
103 |
+
results = response.json()
|
104 |
+
st.write('Results:')
|
105 |
+
annotated_sentence = merge_entities(sentence, results)
|
106 |
+
st.markdown(annotated_sentence, unsafe_allow_html=True)
|
107 |
+
log_to_csv(sentence, results, response_time)
|
108 |
+
|
109 |
+
df = load_response_times()
|
110 |
+
plot_response_times(df)
|
111 |
+
else:
|
112 |
+
st.error("Unable to classify the sentence due to server issues.")
|
113 |
+
else:
|
114 |
+
st.error('Please enter a sentence.')
|
115 |
+
|
116 |
+
#Separate button to just plot the response time
|
117 |
+
if st.button('Show Response Times'):
|
118 |
+
df = load_response_times()
|
119 |
+
plot_response_times(df)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|