Ephraimmm commited on
Commit
dc806a3
·
verified ·
1 Parent(s): f75a2bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -77,9 +77,16 @@ def generate_response(user_message):
77
  full_response += new_token
78
  thread.join()
79
  return full_response
 
80
  # -----------------------------
81
  # 5️⃣ Chat + Save
82
  # -----------------------------
 
 
 
 
 
 
83
 
84
  def save_conversation():
85
  if not chat_history:
@@ -91,7 +98,7 @@ def save_conversation():
91
  conversation.append({"role": "assistant", "content": str(bot_msg)})
92
 
93
  timestamp = time.strftime("%Y%m%d-%H%M%S")
94
- file_path = f"conversation_{timestamp}.txt"
95
 
96
  with open(file_path, "w", encoding="utf-8") as f:
97
  json.dump(conversation, f, indent=4, ensure_ascii=False)
@@ -99,22 +106,22 @@ def save_conversation():
99
  return file_path
100
 
101
  # -----------------------------
102
- # 5️⃣ Gradio interface
103
  # -----------------------------
104
-
105
  with gr.Blocks() as demo:
106
  gr.Markdown("# Nigerian PIDGIN Assistant")
107
  gr.Markdown("Chat with a Nigerian assistant that only speaks Pidgin English.")
108
 
109
- chatbot = gr.Textbox(label="Conversation", lines=15, interactive=False)
110
  user_input = gr.Textbox(label="Your message", placeholder="Type your message here...")
111
 
112
  with gr.Row():
113
  send_button = gr.Button("Send")
114
  save_button = gr.Button("Save Conversation")
 
115
 
116
- send_button.click(generate_response, inputs=user_input, outputs=chatbot)
117
- save_button.click(save_conversation, outputs=gr.File())
118
 
119
  demo.launch()
120
  # iface = gr.Interface(
 
77
  full_response += new_token
78
  thread.join()
79
  return full_response
80
+
81
  # -----------------------------
82
  # 5️⃣ Chat + Save
83
  # -----------------------------
84
+ chat_history = []
85
+
86
+ def chat(user_message):
87
+ bot_response = generate_response(user_message)
88
+ chat_history.append((user_message, bot_response))
89
+ return chat_history, "" # also clears input box
90
 
91
  def save_conversation():
92
  if not chat_history:
 
98
  conversation.append({"role": "assistant", "content": str(bot_msg)})
99
 
100
  timestamp = time.strftime("%Y%m%d-%H%M%S")
101
+ file_path = f"conversation_{timestamp}.txt" # save as TXT not JSON
102
 
103
  with open(file_path, "w", encoding="utf-8") as f:
104
  json.dump(conversation, f, indent=4, ensure_ascii=False)
 
106
  return file_path
107
 
108
  # -----------------------------
109
+ # 6️⃣ Gradio interface
110
  # -----------------------------
 
111
  with gr.Blocks() as demo:
112
  gr.Markdown("# Nigerian PIDGIN Assistant")
113
  gr.Markdown("Chat with a Nigerian assistant that only speaks Pidgin English.")
114
 
115
+ chatbot = gr.Chatbot(label="Conversation")
116
  user_input = gr.Textbox(label="Your message", placeholder="Type your message here...")
117
 
118
  with gr.Row():
119
  send_button = gr.Button("Send")
120
  save_button = gr.Button("Save Conversation")
121
+ download_file = gr.File(label="Download Conversation")
122
 
123
+ send_button.click(chat, inputs=user_input, outputs=[chatbot, user_input])
124
+ save_button.click(save_conversation, outputs=download_file)
125
 
126
  demo.launch()
127
  # iface = gr.Interface(