awacke1 commited on
Commit
b2c602a
Β·
1 Parent(s): 2b51f30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -21
app.py CHANGED
@@ -40,10 +40,58 @@ if (!window.hasBeenRun) {
40
  }
41
  </script>
42
  """
 
43
  with open(os.path.join(gr.networking.STATIC_TEMPLATE_LIB, "frontend", "index.html"), "a") as f:
44
  f.write(SCRIPT)
45
 
 
 
 
 
 
 
 
 
 
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  repo = Repository(
49
  local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
@@ -69,21 +117,61 @@ def generate_html() -> str:
69
  html += "</div>"
70
  return html
71
 
 
72
 
73
- def store_message(name: str, message: str):
74
- if name and message:
75
- with open(DATA_FILE, "a") as csvfile:
76
- writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
77
- writer.writerow(
78
- {"name": name, "message": message, "time": str(datetime.now())}
79
- )
80
- commit_url = repo.push_to_hub()
81
- print(commit_url)
82
 
83
- return generate_html()
84
 
85
 
86
- iface = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  store_message,
88
  [
89
  inputs.Textbox(placeholder="Your name"),
@@ -95,15 +183,8 @@ iface = gr.Interface(
95
  """,
96
  title="Reading/writing to a HuggingFace dataset repo from Spaces",
97
  description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
98
- article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL}) (open in new tab)",
99
- )
100
-
101
- iface.launch()
102
-
103
-
104
-
105
- def gan():
106
- r=store_message()
107
 
108
  images = [
109
  (random.choice(
@@ -147,9 +228,10 @@ def gan():
147
  ), f"label {i}" if i != 0 else "label" * 50)
148
  for i in range(10)
149
  ]
 
 
150
  return images
151
 
152
-
153
  with gr.Blocks() as demo:
154
  with gr.Column(variant="panel"):
155
  [
@@ -166,12 +248,17 @@ with gr.Blocks() as demo:
166
  container=False,
167
  )
168
  btn = gr.Button("Generate image").style(full_width=False)
 
169
 
170
  gallery = gr.Gallery(
171
  label="Generated images", show_label=False, elem_id="gallery"
172
  ).style(grid=[2], height="auto")
 
 
 
173
 
174
  btn.click(gan, None, gallery)
 
175
 
176
  if __name__ == "__main__":
177
  demo.launch()
 
40
  }
41
  </script>
42
  """
43
+
44
  with open(os.path.join(gr.networking.STATIC_TEMPLATE_LIB, "frontend", "index.html"), "a") as f:
45
  f.write(SCRIPT)
46
 
47
+ # -------------------------------------------- For Memory - you will need to set up a dataset and HF_TOKEN ---------
48
+ UseMemory=False
49
+
50
+
51
+ DATASET_REPO_URL="https://huggingface.co/datasets/awacke1/ChatbotMemory.csv"
52
+ DATASET_REPO_ID="awacke1/ChatbotMemory.csv"
53
+ DATA_FILENAME="ChatbotMemory.csv"
54
+ DATA_FILE=os.path.join("data", DATA_FILENAME)
55
+ HF_TOKEN=os.environ.get("HF_TOKEN")
56
 
57
+ if UseMemory:
58
+ try:
59
+ hf_hub_download(
60
+ repo_id=DATASET_REPO_ID,
61
+ filename=DATA_FILENAME,
62
+ cache_dir=DATA_DIRNAME,
63
+ force_filename=DATA_FILENAME
64
+ )
65
+ except:
66
+ print("file not found")
67
+ repo = Repository(
68
+ local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
69
+ )
70
+
71
+ def get_df(name: str):
72
+ dataset = load_dataset(str, split="train")
73
+ return dataset
74
+
75
+ #def store_message(name: str, message: str) -> str:
76
+ def store_message(name: str, message: str):
77
+ if name and message:
78
+ with open(DATA_FILE, "a") as csvfile:
79
+ writer = csv.DictWriter(csvfile, fieldnames=[ "time", "message", "name", ])
80
+ writer.writerow(
81
+ {"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
82
+ )
83
+ commit_url = repo.push_to_hub()
84
+
85
+ # test api retrieval of any dataset that is saved, then return it...
86
+ # app = FastAPI()
87
+ # see: https://gradio.app/sharing_your_app/#api-page
88
+
89
+ # f=get_df(DATASET_REPO_ID)
90
+ # print(f)
91
+ #return commit_url
92
+ return ""
93
+ # ----------------------------------------------- For Memory
94
+
95
 
96
  repo = Repository(
97
  local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
 
117
  html += "</div>"
118
  return html
119
 
120
+ return generate_html()
121
 
 
 
 
 
 
 
 
 
 
122
 
 
123
 
124
 
125
+ def gan():
126
+
127
+ images = [
128
+ (random.choice(
129
+ [
130
+ "Comectress.png",
131
+ "Comic Plate 1 - Amnesia.png",
132
+ "Comic Plate 1 - Depths.png",
133
+ "Comic Plate 1 - Helion.png",
134
+ "Comic Plate 1 - Kitsune.png",
135
+ "Comic Plate 1 - Sinbad.png",
136
+ "Comic Plate 1 - Vampiress.png",
137
+ "Comic Plate 2 - Amnesia.png",
138
+ "Comic Plate 2 - Depths.png",
139
+ "Comic Plate 2 - Helion.png",
140
+ "Comic Plate 2 - Kitsune.png",
141
+ "Comic Plate 2 - Sinbad.png",
142
+ "Comic Plate 2 - Vampiress.png",
143
+ "Comic Plate 3 - Amnesia.png",
144
+ "Comic Plate 3 - Depths.png",
145
+ "Comic Plate 3 - Helion.png",
146
+ "Comic Plate 3 - Kitsune.png",
147
+ "Comic Plate 3 - Vampiress.png",
148
+ "Comic Plate 4 - Amnesia.png",
149
+ "Comic Plate 4 - Depths.png",
150
+ "Comic Plate 4 - Helion.png",
151
+ "Comic Plate 4 - Kitsune.png",
152
+ "Comic Plate 4 - Sinbad.png",
153
+ "Comic Plate 4 - Vampiress.png",
154
+ "Comic Plate 5 - Vampiress.png",
155
+ "Gold Suit.png",
156
+ "Heavens.png",
157
+ "Red Dot Sight.png",
158
+ "Starfire.png",
159
+ "Vamp 1.png",
160
+ "Vamp 2.png",
161
+ "Vamp 3.png",
162
+ "Vamp 4.png",
163
+ "Whirlwind.png",
164
+ "Zyphoria.png",
165
+ ]
166
+ ), f"label {i}" if i != 0 else "label" * 50)
167
+ for i in range(10)
168
+ ]
169
+ return images
170
+
171
+
172
+ def gan2():
173
+ r=store_message()
174
+ iface = gr.Interface(
175
  store_message,
176
  [
177
  inputs.Textbox(placeholder="Your name"),
 
183
  """,
184
  title="Reading/writing to a HuggingFace dataset repo from Spaces",
185
  description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
186
+ article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL}) (open in new tab)",)
187
+
 
 
 
 
 
 
 
188
 
189
  images = [
190
  (random.choice(
 
228
  ), f"label {i}" if i != 0 else "label" * 50)
229
  for i in range(10)
230
  ]
231
+ iface.launch()
232
+
233
  return images
234
 
 
235
  with gr.Blocks() as demo:
236
  with gr.Column(variant="panel"):
237
  [
 
248
  container=False,
249
  )
250
  btn = gr.Button("Generate image").style(full_width=False)
251
+ btn2 = gr.Button("Generate story").style(full_width=False)
252
 
253
  gallery = gr.Gallery(
254
  label="Generated images", show_label=False, elem_id="gallery"
255
  ).style(grid=[2], height="auto")
256
+ gallery2 = gr.Gallery(
257
+ label="Generated images 2", show_label=False, elem_id="gallery"
258
+ ).style(grid=[2], height="auto")
259
 
260
  btn.click(gan, None, gallery)
261
+ btn2.click(gan2, None, gallery2)
262
 
263
  if __name__ == "__main__":
264
  demo.launch()