JaweriaGenAI commited on
Commit
bea35f9
Β·
verified Β·
1 Parent(s): cc20edb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -43
app.py CHANGED
@@ -1,12 +1,10 @@
1
  import os
2
- os.system("pip install -q openai-whisper")
3
-
4
- import gradio as gr
5
  import json, re, uuid
6
  from datetime import datetime
7
  from pydub import AudioSegment
8
  import whisper
9
  import requests
 
10
  import emoji
11
 
12
  # Load Groq API key and Whisper model
@@ -27,7 +25,7 @@ def save_chat_auto(history):
27
  title = re.sub(r"[^\w\s]", "", prompt).strip()
28
  title = " ".join(title.split()[:5]) or "Chat"
29
  timestamp = datetime.now().strftime("%b %d %Y %H-%M-%S")
30
- filename = f"{title} - {timestamp}.json"
31
  with open(os.path.join(CHAT_DIR, filename), "w", encoding="utf-8") as f:
32
  json.dump(history, f, indent=2, ensure_ascii=False)
33
  return filename
@@ -44,27 +42,22 @@ def load_chat_file(filename):
44
  print(f"Load error: {e}")
45
  return [], []
46
 
47
- # Chat handler
48
  def chat_with_groq(message, history):
49
  try:
50
  messages = [{"role": "system", "content": "You are Neobot – a helpful, professional assistant."}]
51
  messages += history + [{"role": "user", "content": message}]
52
-
53
  headers = {"Authorization": f"Bearer {groq_key}", "Content-Type": "application/json"}
54
  payload = {"model": "llama3-70b-8192", "messages": messages}
55
-
56
  res = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
57
  reply = res.json()["choices"][0]["message"]["content"]
58
  reply = filter_emojis(reply)
59
-
60
  history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
61
  save_chat_auto(history)
62
  return "", history, history
63
  except Exception as e:
64
- print(f"Chat error: {e}")
65
- return "", history + [{"role": "assistant", "content": "❌ Error occurred while responding."}], history
66
 
67
- # Transcribe audio
68
  def transcribe_audio(audio_path):
69
  if not audio_path or not os.path.exists(audio_path): return ""
70
  try:
@@ -82,51 +75,35 @@ body { background: white; font-family: 'Segoe UI', sans-serif; }
82
  .gr-button, .gr-textbox { background: #fff; color: #000; }
83
  textarea, input[type='text'] { border: 1px solid #ccc; border-radius: 20px; padding: 10px; height: 48px; }
84
  .sidebar { background: #f5f5f5; height: 100%; overflow-y: auto; padding: 10px; }
85
- .sidebar button { width: 100%; margin: 5px 0; text-align: left; border: 1px solid #ccc; border-radius: 5px; background: white; }
 
86
  """) as demo:
87
 
88
  state = gr.State([])
89
- recording = gr.State(False)
90
-
91
  with gr.Row():
92
  with gr.Column(scale=1):
93
  gr.Markdown("<h2 style='text-align:center;'>Chats</h2>")
94
  new_chat = gr.Button("πŸ†• New Chat")
95
- chat_buttons = gr.Column()
96
-
97
  with gr.Column(scale=3):
98
  gr.Markdown("<h1 style='text-align:center;'>Neobot</h1>")
99
  chatbot = gr.Chatbot(height=400, label="Neobot", type="messages")
100
  with gr.Row():
101
  chat_input = gr.Textbox(placeholder="Type or speak here...", scale=8, show_label=False)
102
- plus = gr.Button("βž•")
103
- mic = gr.Button("πŸŽ™οΈ")
104
- file_input = gr.File(file_types=[".mp3", ".wav"], visible=False)
105
-
106
- def update_chat_list():
107
- buttons = []
108
- for file in list_saved_chats():
109
- filename = file[:-5]
110
- btn = gr.Button(filename, elem_id=f"btn_{filename}", size="sm")
111
- btn.click(fn=lambda f=file: load_chat_file(f), inputs=[], outputs=[chatbot, state])
112
- buttons.append(btn)
113
- return buttons
114
-
115
- send = gr.Button("Send πŸš€")
116
- send.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
117
- chat_input.submit(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
118
 
119
- def start_stop_recording(is_recording):
120
- if not is_recording:
121
- return "Recording...", True
122
- else:
123
- return "", False
124
 
125
  new_chat.click(lambda: ("", [], []), outputs=[chat_input, chatbot, state])
126
- plus.click(lambda: gr.update(visible=True), outputs=[file_input])
127
- file_input.change(transcribe_audio, inputs=file_input, outputs=chat_input)
128
- mic.click(start_stop_recording, inputs=recording, outputs=[chat_input, recording])
129
-
130
- demo.load(update_chat_list, outputs=[])
 
131
 
132
- demo.launch()
 
1
  import os
 
 
 
2
  import json, re, uuid
3
  from datetime import datetime
4
  from pydub import AudioSegment
5
  import whisper
6
  import requests
7
+ import gradio as gr
8
  import emoji
9
 
10
  # Load Groq API key and Whisper model
 
25
  title = re.sub(r"[^\w\s]", "", prompt).strip()
26
  title = " ".join(title.split()[:5]) or "Chat"
27
  timestamp = datetime.now().strftime("%b %d %Y %H-%M-%S")
28
+ filename = f"{title} - {timestamp} - {uuid.uuid4().hex[:6]}.json"
29
  with open(os.path.join(CHAT_DIR, filename), "w", encoding="utf-8") as f:
30
  json.dump(history, f, indent=2, ensure_ascii=False)
31
  return filename
 
42
  print(f"Load error: {e}")
43
  return [], []
44
 
 
45
  def chat_with_groq(message, history):
46
  try:
47
  messages = [{"role": "system", "content": "You are Neobot – a helpful, professional assistant."}]
48
  messages += history + [{"role": "user", "content": message}]
 
49
  headers = {"Authorization": f"Bearer {groq_key}", "Content-Type": "application/json"}
50
  payload = {"model": "llama3-70b-8192", "messages": messages}
 
51
  res = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
52
  reply = res.json()["choices"][0]["message"]["content"]
53
  reply = filter_emojis(reply)
 
54
  history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
55
  save_chat_auto(history)
56
  return "", history, history
57
  except Exception as e:
58
+ error_msg = f"❌ Error: {str(e)}"
59
+ return "", history + [{"role": "assistant", "content": error_msg}], history
60
 
 
61
  def transcribe_audio(audio_path):
62
  if not audio_path or not os.path.exists(audio_path): return ""
63
  try:
 
75
  .gr-button, .gr-textbox { background: #fff; color: #000; }
76
  textarea, input[type='text'] { border: 1px solid #ccc; border-radius: 20px; padding: 10px; height: 48px; }
77
  .sidebar { background: #f5f5f5; height: 100%; overflow-y: auto; padding: 10px; }
78
+ .sidebar button { width: 100%; margin: 5px 0; text-align: left; border: 1px solid #ccc; border-radius: 8px; background: white; transition: all 0.2s ease; }
79
+ .sidebar button:hover { background: #eee; }
80
  """) as demo:
81
 
82
  state = gr.State([])
 
 
83
  with gr.Row():
84
  with gr.Column(scale=1):
85
  gr.Markdown("<h2 style='text-align:center;'>Chats</h2>")
86
  new_chat = gr.Button("πŸ†• New Chat")
87
+ chat_selector = gr.Dropdown(choices=list_saved_chats(), label="Load Saved Chat")
88
+ load_btn = gr.Button("πŸ“‚ Load")
89
  with gr.Column(scale=3):
90
  gr.Markdown("<h1 style='text-align:center;'>Neobot</h1>")
91
  chatbot = gr.Chatbot(height=400, label="Neobot", type="messages")
92
  with gr.Row():
93
  chat_input = gr.Textbox(placeholder="Type or speak here...", scale=8, show_label=False)
94
+ send_btn = gr.Button("πŸš€ Send")
95
+ audio_input = gr.Audio(source="microphone", type="filepath", label="🎀 Voice Input")
96
+ file_input = gr.File(file_types=[".mp3", ".wav"], label="Upload Audio")
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
+ def refresh_chat_list():
99
+ return gr.update(choices=list_saved_chats())
 
 
 
100
 
101
  new_chat.click(lambda: ("", [], []), outputs=[chat_input, chatbot, state])
102
+ send_btn.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
103
+ chat_input.submit(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
104
+ file_input.change(transcribe_audio, inputs=file_input, outputs=[chat_input])
105
+ audio_input.change(transcribe_audio, inputs=audio_input, outputs=[chat_input])
106
+ load_btn.click(lambda name: load_chat_file(name), inputs=[chat_selector], outputs=[chatbot, state])
107
+ demo.load(refresh_chat_list, outputs=[chat_selector])
108
 
109
+ demo.launch()