Spaces:
Runtime error
Runtime error
New version text2int
Browse filesFiles changed (5)
- .gitignore+3
- app.py+155-143
- plot_calls.py+23-3
- test_api.py+3-3
- test_api.sh+41-24
99 unmodified lines10010110299 unmodified lines**/*private*/call_history.csv/call_history.txt99 unmodified lines10010110210310410599 unmodified lines**/*private*/call_history.csv/call_history.txt/output.csv/call_history_bash.csv/call_history_sentiment_bash.csv
12345678910111213141516171819201 unmodified line22232425262728293031323334353637383940415 unmodified lines474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114216 unmodified lines1591601611621631641657 unmodified lines1731741751761771781798 unmodified lines18818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523620 unmodified lines2572582592602612622638 unmodified lines272273274275276277278import inspectimport jsonimport loggingimport osfrom typing import List, Typeimport gradio as grimport spacy # noqafrom dotenv import load_dotenvfrom gradio import routesfrom transformers import pipelineload_dotenv()TOKENS2INT_ERROR_INT = 32202log = logging.getLogger()ONES = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight",1 unmodified line"sixteen", "seventeen", "eighteen", "nineteen",]# token_mapping = json.load(open('str_mapping.json'))CHAR_MAPPING = {"-": " ","_": " ",}CHAR_MAPPING.update((str(i), word) for i, word in enumerate([" " + s + " " for s in ONES]))TOKEN_MAPPING = dict(enumerate([" " + s + " " for s in ONES]))BQ_JSON = os.environ['BQ_JSON']def tokenize(text):return text.split()def detokenize(tokens):5 unmodified linesdef replace_chars(text, char_mapping=CHAR_MAPPING):return ''.join((char_mapping.get(c, c) for c in text))def tokens2int(tokens, numwords={}):""" Convert an English str containing number words into an int>>> text2int("nine")9>>> text2int("forty two")42>>> text2int("1 2 three")123"""if not numwords:tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]scales = ["hundred", "thousand", "million", "billion", "trillion"]numwords["and"] = (1, 0)for idx, word in enumerate(ONES):numwords[word] = (1, idx)for idx, word in enumerate(tens):numwords[word] = (1, idx * 10)for idx, word in enumerate(scales):numwords[word] = (10 ** (idx * 3 or 2), 0)current = result = 0for word in tokens:if word not in numwords:raise Exception("Illegal word: " + word)scale, increment = numwords[word]current = current * scale + incrementif scale > 100:result += currentcurrent = 0return str(result + current)def text2int(text):return tokens2int(tokenize(replace_chars(text)))def try_text2int(text):text = str(text)try:intstr = tokens2int(tokens2int(tokenize(replace_chars(text))))except Exception as e:log.error(str(e))log.error(f'User input: {text}')intstr = TOKENS2INT_ERROR_INTreturn str(intstr)def try_text2int_preprocessed(text):text = str(text)try:tokens = replace_tokens(tokenize(replace_chars(str(text))))except Exception as e:log.error(str(e))tokens = text.split()try:intstr = tokens2int(tokens)except Exception as e:log.error(str(e))intstr = str(TOKENS2INT_ERROR_INT)return intstrdef get_types(cls_set: List[Type], component: str):docset = []types = []if component == "input":for cls in cls_set:doc = inspect.getdoc(cls)doc_lines = doc.split("\n")docset.append(doc_lines[1].split(":")[-1])types.append(doc_lines[1].split(")")[0].split("(")[-1])else:for cls in cls_set:doc = inspect.getdoc(cls)doc_lines = doc.split("\n")docset.append(doc_lines[-1].split(":")[-1])types.append(doc_lines[-1].split(")")[0].split("(")[-1])return docset, typesroutes.get_types = get_typessentiment = pipeline(task="sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")16 unmodified linesbutton_text2int = gr.Button("text2int")button_text2int.click(fn=try_text2int,inputs=inputs_text2int,outputs=outputs_text2int,api_name="text2int",7 unmodified linesgr.Examples(examples=examples_text2int, inputs=inputs_text2int)gr.Markdown(r"""## API```pythonimport requests8 unmodified lines```bashcurl -X POST https://tangibleai-mathtext.hf.space/run/text2int -H 'Content-Type: application/json' -d '{"data": ["one hundred forty five"]}'```{bq_json}""" + f"{json.loads(BQ_JSON)['type']}")with gr.Tab("Text to integer preprocessed"):inputs_text2int_preprocessed = [gr.Text(placeholder="Type a number as text or a sentence", label="Text to process",value="forty two"),]outputs_text2int_preprocessed = gr.Textbox(label="Output integer")button_text2int = gr.Button("text2int preprocessed")button_text2int.click(fn=try_text2int_preprocessed,inputs=inputs_text2int_preprocessed,outputs=outputs_text2int_preprocessed,api_name="text2int-preprocessed",)examples_text2int_preprocessed = ["one thousand forty seven","one hundred",]gr.Examples(examples=examples_text2int_preprocessed, inputs=inputs_text2int_preprocessed)gr.Markdown(r"""## API```pythonimport requestsrequests.post(url="https://tangibleai-mathtext.hf.space/run/text2int-preprocessed", json={"data": ["one hundred forty five"]}).json()```Or using `curl`:```bashcurl -X POST https://tangibleai-mathtext.hf.space/run/text2int-preprocessed -H 'Content-Type: application/json' -d '{"data": ["one hundred forty five"]}'```{bq_json}""" + f"{json.loads(BQ_JSON)['type']}")with gr.Tab("Sentiment Analysis"):inputs_sentiment = [20 unmodified linesgr.Examples(examples=examples_sentiment, inputs=inputs_sentiment)gr.Markdown(r"""## API```pythonimport requests8 unmodified lines```bashcurl -X POST https://tangibleai-mathtext.hf.space/run/sentiment-analysis -H 'Content-Type: application/json' -d '{"data": ["You are right!"]}'```{bq_json}""" + f"{json.loads(BQ_JSON)['type']}")# interface = gr.Interface(lambda x: x, inputs=["text"], outputs=["text"])# html_block.input_components = interface.input_components1234567891011121314151 unmodified line171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970715 unmodified lines777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719816 unmodified lines2152162172182192202217 unmodified lines2292302312322332348 unmodified lines24324424524624724824920 unmodified lines2702712722732742758 unmodified lines284285286287288289290import gradio as grimport spacy # noqafrom transformers import pipeline# import os# os.environ['KMP_DUPLICATE_LIB_OK']='True'# import spacy# Change this according to what words should be corrected toSPELL_CORRECT_MIN_CHAR_DIFF = 2TOKENS2INT_ERROR_INT = 32202ONES = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight",1 unmodified line"sixteen", "seventeen", "eighteen", "nineteen",]CHAR_MAPPING = {"-": " ","_": " ","and": " ",}# CHAR_MAPPING.update((str(i), word) for i, word in enumerate([" " + s + " " for s in ONES]))TOKEN_MAPPING = {"and": " ","oh": "0",}def find_char_diff(a, b):# Finds the character difference between two str objects by counting the occurences of every character. Not edit distance.char_counts_a = {}char_counts_b = {}for char in a:if char in char_counts_a.keys():char_counts_a[char] += 1else:char_counts_a[char] = 1for char in b:if char in char_counts_b.keys():char_counts_b[char] += 1else:char_counts_b[char] = 1char_diff = 0for i in char_counts_a:if i in char_counts_b.keys():char_diff += abs(char_counts_a[i] - char_counts_b[i])else:char_diff += char_counts_a[i]return char_diffdef tokenize(text):text = text.lower()# print(text)text = replace_tokens(''.join(i for i in replace_chars(text)).split())# print(text)text = [i for i in text if i != ' ']# print(text)output = []for word in text:# print(word)output.append(convert_word_to_int(word))output = [i for i in output if i != ' ']# print(output)return outputdef detokenize(tokens):5 unmodified linesdef replace_chars(text, char_mapping=CHAR_MAPPING):return [char_mapping.get(c, c) for c in text]def convert_word_to_int(in_word, numwords={}):# Converts a single word/str into a single inttens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]scales = ["hundred", "thousand", "million", "billion", "trillion"]if not numwords:for idx, word in enumerate(ONES):numwords[word] = idxfor idx, word in enumerate(tens):numwords[word] = idx * 10for idx, word in enumerate(scales):numwords[word] = 10 ** (idx * 3 or 2)if in_word in numwords:# print(in_word)# print(numwords[in_word])return numwords[in_word]try:int(in_word)return int(in_word)except ValueError:pass# Spell correction using find_char_diffchar_diffs = [find_char_diff(in_word, i) for i in ONES + tens + scales]min_char_diff = min(char_diffs)if min_char_diff <= SPELL_CORRECT_MIN_CHAR_DIFF:return char_diffs.index(min_char_diff)def tokens2int(tokens):# Takes a list of tokens and returns a int representation of themtypes = []for i in tokens:if i <= 9:types.append(1)elif i <= 90:types.append(2)else:types.append(3)# print(tokens)if len(tokens) <= 3:current = 0for i, number in enumerate(tokens):if i != 0 and types[i] < types[i - 1] and current != tokens[i - 1] and types[i - 1] != 3:current += tokens[i] + tokens[i - 1]elif current <= tokens[i] and current != 0:current *= tokens[i]elif 3 not in types and 1 not in types:current = int(''.join(str(i) for i in tokens))breakelif '111' in ''.join(str(i) for i in types) and 2 not in types and 3 not in types:current = int(''.join(str(i) for i in tokens))breakelse:current += numberelif 3 not in types and 2 not in types:current = int(''.join(str(i) for i in tokens))else:"""double_list = []current_double = []double_type_list = []for i in tokens:if len(current_double) < 2:current_double.append(i)else:double_list.append(current_double)current_double = []current_double = []for i in types:if len(current_double) < 2:current_double.append(i)else:double_type_list.append(current_double)current_double = []print(double_type_list)print(double_list)current = 0for i, type_double in enumerate(double_type_list):if len(type_double) == 1:current += double_list[i][0]elif type_double[0] == type_double[1]:current += int(str(double_list[i][0]) + str(double_list[i][1]))elif type_double[0] > type_double[1]:current += sum(double_list[i])elif type_double[0] < type_double[1]:current += double_list[i][0] * double_list[i][1]#print(current)"""count = 0current = 0for i, token in enumerate(tokens):count += 1if count == 2:if types[i - 1] == types[i]:current += int(str(token) + str(tokens[i - 1]))elif types[i - 1] > types[i]:current += tokens[i - 1] + tokenelse:current += tokens[i - 1] * tokencount = 0elif i == len(tokens) - 1:current += tokenreturn currentdef text2int(text):# Wraps all of the functions up into onereturn tokens2int(tokenize(text))sentiment = pipeline(task="sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")16 unmodified linesbutton_text2int = gr.Button("text2int")button_text2int.click(fn=text2int,inputs=inputs_text2int,outputs=outputs_text2int,api_name="text2int",7 unmodified linesgr.Examples(examples=examples_text2int, inputs=inputs_text2int)gr.Markdown(r"""## API```pythonimport requests8 unmodified lines```bashcurl -X POST https://tangibleai-mathtext.hf.space/run/text2int -H 'Content-Type: application/json' -d '{"data": ["one hundred forty five"]}'```""")with gr.Tab("Sentiment Analysis"):inputs_sentiment = [20 unmodified linesgr.Examples(examples=examples_sentiment, inputs=inputs_sentiment)gr.Markdown(r"""## API```pythonimport requests8 unmodified lines```bashcurl -X POST https://tangibleai-mathtext.hf.space/run/sentiment-analysis -H 'Content-Type: application/json' -d '{"data": ["You are right!"]}'```""")# interface = gr.Interface(lambda x: x, inputs=["text"], outputs=["text"])# html_block.input_components = interface.input_components
123456789import matplotlib.pyplot as pltimport pandas as pddf = pd.read_csv('call_history.csv') # data loadingprint(df)df.plot(by='endpoint', column='delay', kind='box', showmeans=True)plt.show()1234567891011121314151617181920212223242526272829from datetime import datetimeimport matplotlib.pyplot as pltimport pandas as pd# pd.set_option('display.max_columns', None)# pd.set_option('display.max_rows', None)df = pd.read_csv(filepath_or_buffer='call_history_bash.csv', sep=";")df["elapsed"] = df["finished"].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f")) - df["started"].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f"))df["elapsed"] = df["elapsed"].apply(lambda x: x.total_seconds())df.to_csv("output.csv", index=False, sep=";")student_numbers = df['active_students'].unique()plt.figure(figsize=(16, 10))rows = len(student_numbers)for index, student_number in enumerate(student_numbers, 1):data = df[df["active_students"] == student_number]plt.subplot(rows, 2, 2 * index - 1)plt.title("y=seconds, x=active students", x=0.75, y=0.75)plt.boxplot(x=data["elapsed"], labels=[student_number])plt.subplot(rows, 2, 2 * index)plt.title("y=count of seconds, x=seconds", x=0.75, y=0.75)plt.hist(x=data["elapsed"], bins=25, edgecolor='white')plt.show()
6 unmodified lines7891011121314151617186 unmodified linesimport httpxfrom os.path import existsNUMBER_OF_CALLS = 20headers = {"Content-Type": "application/json; charset=utf-8"}base_url = "https://tangibleai-mathtext.hf.space/run/{endpoint}"# base_url = "http://localhost:7860/run/{endpoint}"data_list_1 = {"endpoint": "text2int",6 unmodified lines7891011121314151617186 unmodified linesimport httpxfrom os.path import existsNUMBER_OF_CALLS = 1headers = {"Content-Type": "application/json; charset=utf-8"}# base_url = "https://tangibleai-mathtext.hf.space/run/{endpoint}"base_url = "http://localhost:7860/run/{endpoint}"data_list_1 = {"endpoint": "text2int",
1234516 unmodified lines222324252627282930313233343536373839404142434445464748495051525354555657585960#! /bin/env bashdata_list_1() {responses=("one hundred forty five"16 unmodified linesecho "${responses[$1]}"}text2int="https://tangibleai-mathtext.hf.space/run/text2int"text2intpreprocessed="https://tangibleai-mathtext.hf.space/run/text2int-preprocessed"sentimentanalysis="https://tangibleai-mathtext.hf.space/run/sentiment-analysis"test_endpoint() {start_=$(date +%s.%N)response=$(curl --silent -X POST "$1" -H 'Content-Type: application/json' -d "$2")end_=$(date +%s.%N)diff=$(echo "$end_ - $start_" | bc)printf " endpoint:%s\n data:%s delay:%s:\n %s\n" "$1" "$2" "$diff" "$response"}echo "start: $(date)"for i in {1..20}; dorandom_value=$((RANDOM % 5))text=$(data_list_1 $random_value)data='{"data": ["'$text'"]}'test_endpoint "$text2int" "$data" >>call_history.txt &donefor i in {1..20}; dorandom_value=$((RANDOM % 5))text=$(data_list_1 $random_value)data='{"data": ["'$text'"]}'test_endpoint "$text2intpreprocessed" "$data" >>call_history.txt &donefor i in {1..20}; dorandom_value=$((RANDOM % 5))text=$(data_list_2 $random_value)data='{"data": ["'$text'"]}'test_endpoint "$sentimentanalysis" "$data" >>call_history.txt &donewait1234567891011121316 unmodified lines303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677#! /bin/env bashLOG_FILE_NAME="call_history_bash.csv"if [[ ! -f "$LOG_FILE_NAME" ]]; then# Creation of column names if the file does not exitsecho "student_id;active_students;endpoint;inputs;outputs;started;finished" > $LOG_FILE_NAMEfidata_list_1() {responses=("one hundred forty five"16 unmodified linesecho "${responses[$1]}"}# endpoints: "text2int" "text2int-preprocessed" "sentiment-analysis"# selected endpoint to testendpoint="text2int"create_random_delay () {# creates a random delay for given argumentsecho "scale=8; $RANDOM/32768*$1" | bc}simulate_student() {# Student simulator waits randomly between 0-10s after an interaction.# Based on 100 interactions per studentfor i in {1..100}; dostart_=$(date +"%F %T.%6N")url="https://tangibleai-mathtext.hf.space/run/$3"response=$(curl --silent -X POST "$url" -H 'Content-Type: application/json' -d "$4")if [[ "$response" == *"504"* ]]; thenresponse="504 Gateway Time-out"fiend_=$(date +"%F %T.%6N")printf "%s;%s;%s;%s;%s;%s;%s\n" "$1" "$2" "$3" "$4" "$response" "$start_" "$end_" >>$LOG_FILE_NAMEsleep "$(create_random_delay 10)"done}echo "start: $(date)"active_students=250 # the number of students using the system at the same timei=1while [[ "$i" -le "$active_students" ]]dorandom_value=$((RANDOM % 5))text=$(data_list_2 $random_value)data='{"data": ["'$text'"]}'simulate_student "student$i" "$active_students" "$endpoint" "$data" &sleep "$(create_random_delay 1)" # adding a random delay between studentsi=$(( "$i" + 1 ))donewait
.gitignore+3
app.py+155-143
plot_calls.py+23-3
test_api.py+3-3
test_api.sh+41-24