merve HF staff commited on
Commit
c0c6018
โ€ข
1 Parent(s): 6e37d4d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import HfApi
3
+ import os
4
+ import datasets
5
+ from datasets import load_dataset, Dataset
6
+ import pandas as pd
7
+
8
+ TOKEN = os.getenv("TOKEN")
9
+ DATASET_ID = os.getenv("DATASET_ID")
10
+ datasets.set_caching_enabled(False)
11
+ hf_api = HfApi(token=TOKEN)
12
+
13
+ def create_dataset():
14
+ # creates the dataset if it doesn't exist
15
+ try:
16
+ hf_api.create_repo(repo_id = DATASET_ID, repo_type = "dataset", private=True)
17
+ print("created dataset")
18
+ initial_dataset()
19
+ except:
20
+ pass
21
+
22
+ def initial_dataset():
23
+ # creates initial dataset repository and uploads csv file
24
+ df = pd.DataFrame(columns=["question", "answer"])
25
+ df.loc[len(df)] = ["Find question here", "Find answer here"] # Add first row
26
+ # persist initial dataset
27
+ df.to_csv("persisted_dataset.csv", index=False)
28
+ # push to hub
29
+ hf_api.upload_file(path_or_fileobj="persisted_dataset.csv",
30
+ path_in_repo = "./persisted_dataset.csv",
31
+ repo_id = DATASET_ID, repo_type="dataset")
32
+
33
+
34
+ def write_to_dataset(input):
35
+ # if the dataset is not created, submit button will crash to we create it
36
+ create_dataset()
37
+ # load the dataset, append input and placeholder
38
+ dataset = load_dataset(DATASET_ID, data_files="persisted_dataset.csv", use_auth_token=TOKEN)
39
+ df = pd.DataFrame(dataset["train"])
40
+ df = df.append({'question': input, "answer":"ANSWER HERE"}, ignore_index=True)
41
+ df = df.drop_duplicates()
42
+ # persist it to local dataset and push it back to Hub
43
+ df.to_csv("persisted_dataset.csv", index=False)
44
+ hf_api.upload_file(path_or_fileobj="persisted_dataset.csv",
45
+ path_in_repo = "./persisted_dataset.csv",
46
+ repo_id = DATASET_ID,
47
+ repo_type="dataset")
48
+ # remove the persisted dataset for privacy
49
+ # as of now we don't persist the dataset anyway so let's see if it will stay without removing
50
+ # os.remove("persisted_dataset.csv")
51
+ return df
52
+
53
+ def read_dataset():
54
+ # read the dataset for second tab, will crash if it doesn't exist
55
+ create_dataset()
56
+ dataset = load_dataset(DATASET_ID, data_files="persisted_dataset.csv", use_auth_token=TOKEN, download_mode='force_redownload')
57
+ # read it to a pandas df
58
+ df = pd.DataFrame(dataset["train"])
59
+ #ย return only answered questions
60
+ return df[df['answer'] != 'ANSWER HERE']
61
+
62
+ def render_answers():
63
+ # render dataframe into sequentially written text
64
+ # used for textbox in second tab
65
+ df = read_dataset()
66
+ new_df = df.copy()
67
+ new_df = new_df.sort_index(ascending=False)
68
+ new_df['question'] = 'โ“ ' + new_df['question']
69
+
70
+ new_df['answer'] = '๐Ÿ™‹ ' + new_df['answer']
71
+
72
+ str_df = '\n'.join(new_df.apply(lambda x: x['question'] + '\n' + x['answer'] + '\n\n\n', axis=1))
73
+
74
+ return str_df
75
+
76
+
77
+ with gr.Blocks() as demo:
78
+ gr.Markdown("## Ask me anything, I'm not going to lie! ๐Ÿ‘‹ ")
79
+ gr.Markdown("In this app, you can write me something anonymous and read my answers to your inputs. โœ๏ธ ")
80
+ gr.Markdown("Let's spread love and be respectful ๐Ÿ’ƒ๐Ÿป๐Ÿ•บ๐Ÿป")
81
+ with gr.Accordion("Open this toggle to see how you can build your own Ask Me Anything app to receive and answer questions โฌ‡๏ธ", open=False):
82
+ gr.Markdown("Duplicate this Space by clicking three dots and then `Duplicate this Space` and provide TOKEN and DATASET_ID.")
83
+ gr.Markdown("Provide your Hugging Face write token that you can get [here](https://huggingface.co/settings/tokens).")
84
+ gr.Markdown("For DATASET_ID, provide something like `merve/answers` it's a dataset repository that will be created through this Space automatically, where your questions will be privately populated.")
85
+ gr.Markdown("To answer questions, simply edit the `persisted_dataset.csv` file in the dataset repository, edit `ANSWER HERE` parts beside every question.")
86
+ gr.Markdown("The app will not show unanswered questions by default.")
87
+
88
+ with gr.Tab("Write me something โœ๏ธ "):
89
+ input = gr.Textbox(label="What do you want to say to me?")
90
+ submit_btn = gr.Button("Submit")
91
+ submit_btn.click(fn=write_to_dataset, inputs=[input])
92
+ with gr.Tab("Read My Answers"):
93
+ answers = render_answers()
94
+ gr.Markdown(answers)
95
+ refresh_btn = gr.Button("Refresh")
96
+ refresh_btn.click(fn=render_answers)
97
+
98
+ demo.launch(debug=True)