sjw commited on
Commit
9d70015
·
1 Parent(s): 6bd6839

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
app.py CHANGED
@@ -1,9 +1,11 @@
1
  import os
2
  import csv
 
 
3
  import gradio as gr
4
  from gradio import components
5
  from huggingface_hub import Repository, hf_hub_download
6
- from datetime import datetime
7
 
8
  DATASET_REPO_URL = "https://huggingface.co/datasets/sjw/data.csv"
9
  DATASET_REPO_ID = "sjw/data.csv"
@@ -20,44 +22,44 @@ try:
20
  force_filename=DATA_FILENAME
21
  )
22
  except:
23
- print("file not found")
 
24
 
25
  repo = Repository(
26
- local_dir = DIRNAME, clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
 
 
27
  )
28
 
29
 
30
  def generate_html() -> str:
 
31
  with open(DATA_FILE) as csvfile:
32
  reader = csv.DictReader(csvfile)
33
- rows = []
34
- for row in reader:
35
- rows.append(row)
36
  rows.reverse()
37
- if len(rows) == 0:
38
- return "no messages yet"
39
- else:
40
- html = "<div class='chatbot'>"
41
- for row in rows:
42
- html += "<div>"
43
- html += f"<span><b>{row['name']}</b></span> " # Make the name bold and add a space after it
44
- html += f"<span class='message'>{row['message']}</span>"
45
- html += "</div>"
46
- html += "</div>"
47
- return html
48
-
49
- def store_message(name: str, message: str):
50
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  if name and message:
52
  with open(DATA_FILE, "a") as csvfile:
53
  writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
54
- writer.writerow(
55
- {"name": name, "message": message, "time": str(datetime.now())}
56
- )
57
  return generate_html()
58
 
59
- with gr.Blocks() as demo:
60
- gr.Markdown("# Title")
 
61
  name_input = components.Textbox(label="Your Username")
62
  message_input = components.Textbox(label="Your Feedback", lines=2)
63
  output_html = gr.HTML()
@@ -65,4 +67,4 @@ with gr.Blocks() as demo:
65
 
66
  submit_button.click(store_message, inputs=[name_input, message_input], outputs=output_html)
67
 
68
- demo.launch()
 
1
  import os
2
  import csv
3
+ from datetime import datetime
4
+
5
  import gradio as gr
6
  from gradio import components
7
  from huggingface_hub import Repository, hf_hub_download
8
+
9
 
10
  DATASET_REPO_URL = "https://huggingface.co/datasets/sjw/data.csv"
11
  DATASET_REPO_ID = "sjw/data.csv"
 
22
  force_filename=DATA_FILENAME
23
  )
24
  except:
25
+ print("File not found")
26
+
27
 
28
  repo = Repository(
29
+ local_dir=DIRNAME,
30
+ clone_from=DATASET_REPO_URL,
31
+ use_auth_token=HF_TOKEN
32
  )
33
 
34
 
35
  def generate_html() -> str:
36
+ """Generate HTML content for the chat."""
37
  with open(DATA_FILE) as csvfile:
38
  reader = csv.DictReader(csvfile)
39
+ rows = [row for row in reader]
 
 
40
  rows.reverse()
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ if not rows:
43
+ return "No messages yet"
44
+
45
+ html = "<div class='chatbot'>"
46
+ for row in rows:
47
+ html += f"<div><span><b>{row['name']}</b></span> <span class='message'>{row['message']}</span></div>"
48
+ html += "</div>"
49
+ return html
50
+
51
+
52
+ def store_message(name: str, message: str) -> str:
53
+ """Store the message and regenerate HTML content."""
54
  if name and message:
55
  with open(DATA_FILE, "a") as csvfile:
56
  writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
57
+ writer.writerow({"name": name, "message": message, "time": str(datetime.now())})
 
 
58
  return generate_html()
59
 
60
+
61
+ with gr.Blocks() as feedback_page:
62
+ gr.Markdown("# User Feedback")
63
  name_input = components.Textbox(label="Your Username")
64
  message_input = components.Textbox(label="Your Feedback", lines=2)
65
  output_html = gr.HTML()
 
67
 
68
  submit_button.click(store_message, inputs=[name_input, message_input], outputs=output_html)
69
 
70
+ feedback_page.launch()