Spaces:
Runtime error
Runtime error
Commit
•
468c4fa
1
Parent(s):
a9833d2
Upload 3 files
Browse files- app.py +89 -0
- requirements.txt +3 -0
- theme.py +95 -0
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding: utf-8
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from theme import CustomTheme
|
5 |
+
import pandas as pd
|
6 |
+
import time
|
7 |
+
import openai
|
8 |
+
import os
|
9 |
+
|
10 |
+
|
11 |
+
custom_theme = CustomTheme()
|
12 |
+
|
13 |
+
|
14 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
15 |
+
|
16 |
+
ft_model_ada = 'ada:ft-personal-2023-05-17-17-03-33'
|
17 |
+
ft_model_davinci = 'ada:ft-personal-2023-05-17-17-03-33'
|
18 |
+
|
19 |
+
def upload_file(file, model_name):
|
20 |
+
|
21 |
+
with open(file.name, "r", encoding='unicode_escape') as f:
|
22 |
+
content = f.read().replace('\n', '')
|
23 |
+
|
24 |
+
sentences = list(map(str.strip, content.split(".")))
|
25 |
+
df = pd.DataFrame({"claim": sentences}, index=range(len(sentences)))
|
26 |
+
# df = pd.DataFrame({"claim": sentences[:10]}, index=range(len(sentences[:10])))
|
27 |
+
|
28 |
+
col = f"openai_pred_{model_name}"
|
29 |
+
model = ft_model_ada if model_name == "ada" else ft_model_davinci
|
30 |
+
|
31 |
+
results = []
|
32 |
+
|
33 |
+
start_time = time.time()
|
34 |
+
|
35 |
+
for i in range(len(sentences)):
|
36 |
+
res = openai.Completion.create(model=model, prompt= sentences[i] + ' ->', max_tokens=1, temperature=0)
|
37 |
+
results.append(res['choices'][0]['text'])
|
38 |
+
|
39 |
+
end_time = time.time()
|
40 |
+
|
41 |
+
# results = sentences[:10]
|
42 |
+
|
43 |
+
df[col] = results
|
44 |
+
df[col] = df[col].map({" True": 1, " False": 0})
|
45 |
+
|
46 |
+
time_taken = round(end_time - start_time, 2)
|
47 |
+
|
48 |
+
display_df_openai_ada = df.loc[df[col] == 1]
|
49 |
+
|
50 |
+
return display_df_openai_ada, time_taken
|
51 |
+
|
52 |
+
|
53 |
+
def export_csv(d, file):
|
54 |
+
filename = file.name.split("/")[-1].split(".")[0]
|
55 |
+
d.to_csv(f"{filename}_output.csv")
|
56 |
+
# filename = file.name.split(".")[0]
|
57 |
+
return gr.File.update(value = f"{filename}_output.csv", visible=True)
|
58 |
+
|
59 |
+
with gr.Blocks(theme=custom_theme) as demo:
|
60 |
+
|
61 |
+
gr.Markdown(value="# Upload transcription txt file")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
input_document_pdf = gr.File(label="Uplaod transcription file")
|
65 |
+
|
66 |
+
with gr.Row():
|
67 |
+
|
68 |
+
with gr.Column():
|
69 |
+
model_name_ada = gr.Text(label="model_name", value="ada", visible=False)
|
70 |
+
output_df = gr.Dataframe(row_count = (2, "fixed"), col_count=(2, "fixed"), headers=["claim", "prediction"],
|
71 |
+
label="Predictions", max_rows=3, max_cols=2, wrap= True)
|
72 |
+
|
73 |
+
time_taken = gr.Text(label="Time Taken", lines=1)
|
74 |
+
download = gr.Button("Download output", variant="primary")
|
75 |
+
|
76 |
+
csv = gr.File(interactive=False, visible=False)
|
77 |
+
|
78 |
+
input_document_pdf.upload(
|
79 |
+
upload_file,
|
80 |
+
[input_document_pdf, model_name_ada],
|
81 |
+
[output_df, time_taken]
|
82 |
+
)
|
83 |
+
|
84 |
+
download.click(export_csv, [output_df, input_document_pdf], [csv])
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
if __name__ == "__main__":
|
89 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
openai
|
3 |
+
pandas
|
theme.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
from typing import Iterable
|
3 |
+
import gradio as gr
|
4 |
+
from gradio.themes.base import Base
|
5 |
+
from gradio.themes import Soft
|
6 |
+
from gradio.themes.utils import colors, fonts, sizes
|
7 |
+
|
8 |
+
|
9 |
+
class CustomTheme(Base):
|
10 |
+
def __init__(
|
11 |
+
self,
|
12 |
+
*,
|
13 |
+
primary_hue: colors.Color | str = colors.blue,
|
14 |
+
secondary_hue: colors.Color | str = colors.cyan,
|
15 |
+
neutral_hue: colors.Color | str = colors.zinc,
|
16 |
+
spacing_size: sizes.Size | str = sizes.spacing_md,
|
17 |
+
radius_size: sizes.Size | str = sizes.radius_md,
|
18 |
+
text_size: sizes.Size | str = sizes.text_md,
|
19 |
+
):
|
20 |
+
super().__init__(
|
21 |
+
primary_hue=primary_hue,
|
22 |
+
secondary_hue=secondary_hue,
|
23 |
+
neutral_hue=neutral_hue,
|
24 |
+
spacing_size=spacing_size,
|
25 |
+
radius_size=radius_size,
|
26 |
+
text_size=text_size,
|
27 |
+
)
|
28 |
+
self.name = "custom_theme"
|
29 |
+
super().set(
|
30 |
+
# Colors
|
31 |
+
background_fill_primary="*neutral_50",
|
32 |
+
slider_color="*primary_500",
|
33 |
+
slider_color_dark="*primary_600",
|
34 |
+
# Shadows
|
35 |
+
shadow_drop="0 1px 4px 0 rgb(0 0 0 / 0.1)",
|
36 |
+
shadow_drop_lg="0 2px 5px 0 rgb(0 0 0 / 0.1)",
|
37 |
+
# Block Labels
|
38 |
+
block_background_fill="white",
|
39 |
+
block_label_padding="*spacing_sm *spacing_md",
|
40 |
+
block_label_background_fill="*primary_100",
|
41 |
+
block_label_background_fill_dark="*primary_600",
|
42 |
+
block_label_radius="*radius_md",
|
43 |
+
block_label_text_size="*text_md",
|
44 |
+
block_label_text_weight="600",
|
45 |
+
block_label_text_color="*primary_500",
|
46 |
+
block_label_text_color_dark="*white",
|
47 |
+
block_title_radius="*block_label_radius",
|
48 |
+
block_title_padding="*block_label_padding",
|
49 |
+
block_title_background_fill="*block_label_background_fill",
|
50 |
+
block_title_text_weight="600",
|
51 |
+
block_title_text_color="*primary_500",
|
52 |
+
block_title_text_color_dark="*white",
|
53 |
+
block_label_margin="*spacing_md",
|
54 |
+
block_shadow="*shadow_drop_lg",
|
55 |
+
# Inputs
|
56 |
+
input_border_color="*neutral_50",
|
57 |
+
input_shadow="*shadow_drop",
|
58 |
+
input_shadow_focus="*shadow_drop_lg",
|
59 |
+
checkbox_shadow="none",
|
60 |
+
# Buttons
|
61 |
+
shadow_spread="6px",
|
62 |
+
button_shadow="*shadow_drop_lg",
|
63 |
+
button_shadow_hover="*shadow_drop_lg",
|
64 |
+
button_shadow_active="*shadow_inset",
|
65 |
+
button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
|
66 |
+
button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
|
67 |
+
button_primary_text_color="white",
|
68 |
+
button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
|
69 |
+
button_primary_background_fill_hover_dark="*primary_500",
|
70 |
+
button_secondary_background_fill="white",
|
71 |
+
button_secondary_background_fill_hover="*neutral_100",
|
72 |
+
button_secondary_background_fill_hover_dark="*primary_500",
|
73 |
+
button_secondary_text_color="*neutral_800",
|
74 |
+
button_cancel_background_fill="*button_secondary_background_fill",
|
75 |
+
button_cancel_background_fill_hover="*button_secondary_background_fill_hover",
|
76 |
+
button_cancel_background_fill_hover_dark="*button_secondary_background_fill_hover",
|
77 |
+
button_cancel_text_color="*button_secondary_text_color",
|
78 |
+
# checkboxes
|
79 |
+
checkbox_label_shadow="*shadow_drop_lg",
|
80 |
+
checkbox_label_background_fill_selected="*primary_500",
|
81 |
+
checkbox_label_background_fill_selected_dark="*primary_600",
|
82 |
+
checkbox_border_width="1px",
|
83 |
+
checkbox_border_color="*neutral_100",
|
84 |
+
checkbox_border_color_dark="*neutral_600",
|
85 |
+
checkbox_background_color_selected="*primary_600",
|
86 |
+
checkbox_background_color_selected_dark="*primary_700",
|
87 |
+
checkbox_border_color_focus="*primary_500",
|
88 |
+
checkbox_border_color_focus_dark="*primary_600",
|
89 |
+
checkbox_border_color_selected="*primary_600",
|
90 |
+
checkbox_border_color_selected_dark="*primary_700",
|
91 |
+
checkbox_label_text_color_selected="white",
|
92 |
+
# Borders
|
93 |
+
block_border_width="0px",
|
94 |
+
panel_border_width="1px",
|
95 |
+
)
|