Spaces:
Sleeping
Sleeping
added app
Browse files- app.py +58 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import pathlib
|
| 4 |
+
|
| 5 |
+
def show_answer(source_html,answer_tsv):
|
| 6 |
+
questions = []
|
| 7 |
+
options = []
|
| 8 |
+
images = []
|
| 9 |
+
with open(source_html) as fp:
|
| 10 |
+
soup = BeautifulSoup(fp, 'html.parser')
|
| 11 |
+
q = soup.find_all("div",{"class":"field"})
|
| 12 |
+
for a in q:
|
| 13 |
+
option = a.find_all("div",{"class": "ui-radio"})
|
| 14 |
+
option = [o.get_text(strip=True) for o in option]
|
| 15 |
+
if len(option) < 2:
|
| 16 |
+
continue
|
| 17 |
+
while len(option) < 4:
|
| 18 |
+
option.append("から")
|
| 19 |
+
options.append(option)
|
| 20 |
+
questions.append(a.find_all("div",{"class": "topichtml"})[0].get_text(strip=True))
|
| 21 |
+
images.append([x['src'].split('/')[-1] for x in a.find_all("img")])
|
| 22 |
+
|
| 23 |
+
import pandas as pd
|
| 24 |
+
df = pd.DataFrame()
|
| 25 |
+
|
| 26 |
+
import re
|
| 27 |
+
questions = [re.sub( r"\d*\. ", "",q ).strip() for q in questions]
|
| 28 |
+
option_1 = [q[0].strip() for q in options]
|
| 29 |
+
option_2 = [q[1].strip() for q in options]
|
| 30 |
+
option_3 = [q[2].strip() for q in options]
|
| 31 |
+
option_4 = [q[3].strip() for q in options]
|
| 32 |
+
image = images
|
| 33 |
+
df["questions"] = questions
|
| 34 |
+
df["option_1"] = option_1
|
| 35 |
+
df["option_2"] = option_2
|
| 36 |
+
df["option_3"] = option_3
|
| 37 |
+
df["option_4"] = option_4
|
| 38 |
+
answer_df = pd.read_csv(answer_tsv,sep="\t",index_col=0)
|
| 39 |
+
answers = []
|
| 40 |
+
for idx,row in df.iterrows():
|
| 41 |
+
answer = answer_df[
|
| 42 |
+
answer_df['questions'].str.contains(row['questions']) & \
|
| 43 |
+
answer_df['option_1'].str.contains(row['option_1']) & \
|
| 44 |
+
answer_df['option_2'].str.contains(row['option_2']) & \
|
| 45 |
+
answer_df['option_3'].str.contains(row['option_3']) & \
|
| 46 |
+
answer_df['option_4'].str.contains(row['option_4']) \
|
| 47 |
+
]['answer'].values
|
| 48 |
+
answers.append(f"{idx+1}\t{str(answer)} \n {'-'*20}")
|
| 49 |
+
return "\n".join(answers)
|
| 50 |
+
|
| 51 |
+
inputs = [
|
| 52 |
+
gradio.UploadButton("HTMLをアップロード!"),
|
| 53 |
+
gradio.UploadButton("答えのtsvをアップロード!"),
|
| 54 |
+
]
|
| 55 |
+
outputs = gradio.Text()
|
| 56 |
+
|
| 57 |
+
demo = gradio.Interface(fn=show_answer, inputs=inputs, outputs=outputs)
|
| 58 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
gradio
|
| 3 |
+
beautifulsoup4
|