JaweriaGenAI commited on
Commit
d1b380c
Β·
verified Β·
1 Parent(s): 26d7fc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -51
app.py CHANGED
@@ -1,21 +1,24 @@
 
 
 
1
  import gradio as gr
2
- import os, json, re, uuid
3
  from datetime import datetime
4
  from pydub import AudioSegment
5
  import whisper
6
  import requests
7
  import emoji
8
 
9
- # Load keys and models
10
  groq_key = os.getenv("GROQ_API_KEY")
11
  whisper_model = whisper.load_model("base")
12
 
13
- # Emoji filter for assistant replies
14
  def filter_emojis(text):
15
- allowed = {"😊", "πŸ˜„", "πŸ‘", "πŸ€–", "✨", "πŸŽ‰", "πŸ’¬", "πŸ™Œ", "😎", "πŸ“’", "🧠", "βœ…"}
16
- return "".join(char if char not in emoji.EMOJI_DATA or char in allowed else "" for char in text)
17
 
18
- # Chat function using Groq API
19
  def chat_with_groq(message, history):
20
  messages = [{"role": "system", "content": "You are JAWERIA'SBOT πŸ€– – cheerful, emoji-savvy, and sleek."}]
21
  messages += history + [{"role": "user", "content": message}]
@@ -24,17 +27,23 @@ def chat_with_groq(message, history):
24
  "Authorization": f"Bearer {groq_key}",
25
  "Content-Type": "application/json"
26
  }
27
- payload = {"model": "llama3-70b-8192", "messages": messages}
28
- res = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
29
- reply = filter_emojis(res.json()["choices"][0]["message"]["content"])
 
 
 
 
 
 
30
 
31
  history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
32
  return "", history, history
33
 
34
- # Transcribe audio from mic or file
35
  def transcribe_audio(audio_path):
36
- if not audio_path or not os.path.exists(audio_path):
37
- return "⚠️ No audio available."
38
  try:
39
  temp_wav = f"{uuid.uuid4()}.wav"
40
  AudioSegment.from_file(audio_path).export(temp_wav, format="wav")
@@ -44,11 +53,13 @@ def transcribe_audio(audio_path):
44
  except Exception as e:
45
  return f"❌ Transcription error: {e}"
46
 
47
- # Save/load chat sessions
48
  def save_session(history):
49
- title = re.sub(r"[^\w\s]", "", next((m["content"] for m in history if m["role"] == "user"), "chat")).strip()
 
50
  title = " ".join(title.split()[:6])
51
- filename = f"{title} - {datetime.now().strftime('%b %d %Y %H-%M')}.json"
 
52
  with open(filename, "w", encoding="utf-8") as f:
53
  json.dump(history, f, indent=2, ensure_ascii=False)
54
  return f"βœ… Saved `{filename[:-5]}`"
@@ -57,69 +68,74 @@ def list_saved_files():
57
  return sorted([f[:-5] for f in os.listdir() if f.endswith(".json")])
58
 
59
  def load_chat(name):
 
60
  try:
61
- with open(f"{name}.json", "r", encoding="utf-8") as f:
62
  history = json.load(f)
63
  return history, history, f"βœ… Loaded `{name}`"
64
  except Exception as e:
65
  return [], [], f"❌ Load error: {e}"
66
 
67
- # Interface
68
  with gr.Blocks(css="""
69
- body { background: #111; color: white; font-family: 'Segoe UI'; }
 
 
70
  textarea, input[type='text'] {
71
- background-color: #222; color: white; border-radius: 30px;
72
- padding: 10px 15px; border: 1px solid #444; height: 48px;
73
  }
74
  .gr-button {
75
- background-color: #333 !important; color: white !important;
76
- border: 1px solid #444; border-radius: 10px; font-weight: bold;
 
77
  }
78
- .gr-button:hover { background: #555 !important; }
79
- .gr-dropdown { background: #222; color: white; max-height: 200px; overflow-y: auto; }
80
- .gr-chatbot-message { background: #292929 !important; color: white; border-radius: 10px; }
81
  """) as demo:
82
 
83
  state = gr.State([])
 
 
 
 
 
 
84
  with gr.Row():
85
- chat_input = gr.Textbox(placeholder="Type or speak...", label=None, scale=9)
86
- record_btn = gr.Button("πŸŽ™οΈ Record", scale=1)
87
- upload_btn = gr.Audio(source="upload", type="filepath", visible=False)
88
 
89
- chatbot = gr.Chatbot(label="JAWERIA'SBOT πŸ€–", height=400)
90
  send_btn = gr.Button("Send πŸš€")
91
 
 
 
92
  with gr.Row():
93
- new_btn = gr.Button("πŸ†• New")
94
  save_btn = gr.Button("πŸ’Ύ Save")
95
- dropdown = gr.Dropdown(label="πŸ“‚ Load Saved", choices=list_saved_files(), interactive=True)
96
  load_btn = gr.Button("πŸ“₯ Load")
97
 
98
  save_msg = gr.Markdown()
99
  load_msg = gr.Markdown()
100
 
101
- recording = gr.Audio(source="microphone", type="filepath", visible=False)
102
-
103
- def toggle_recording(is_recording):
104
- return not is_recording, gr.update(visible=not is_recording)
105
-
106
- def start_record(): return gr.update(visible=True)
107
- def stop_record(path): return transcribe_audio(path)
108
-
109
- record_state = gr.State(False)
110
- record_btn.click(start_record, outputs=recording).then(
111
- fn=lambda: True, outputs=record_state)
112
- recording.change(fn=stop_record, inputs=recording, outputs=chat_input)
113
-
114
- upload_btn.change(fn=transcribe_audio, inputs=upload_btn, outputs=chat_input)
115
  send_btn.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
116
  chat_input.submit(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
117
- new_btn.click(lambda: ("", [], []), outputs=[chat_input, chatbot, state])
118
- save_btn.click(save_session, inputs=[state], outputs=[save_msg])
119
- save_btn.click(list_saved_files, outputs=[dropdown])
120
- load_btn.click(load_chat, inputs=[dropdown], outputs=[chatbot, state, load_msg])
121
- demo.load(list_saved_files, outputs=[dropdown])
122
 
123
- gr.Markdown("<center><small>Made with 🀍 by Jaweria</small></center>")
 
 
 
 
 
 
 
 
 
124
 
125
  demo.launch()
 
 
1
+ import os
2
+ os.system("pip install -q git+https://github.com/openai/whisper.git")
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 API key
13
  groq_key = os.getenv("GROQ_API_KEY")
14
  whisper_model = whisper.load_model("base")
15
 
16
+ # βœ… Safe emoji filtering
17
  def filter_emojis(text):
18
+ allowed_emojis = {"😊", "πŸ˜„", "πŸ‘", "πŸ€–", "✨", "πŸŽ‰", "πŸ’¬", "πŸ™Œ", "😎", "πŸ“’", "🧠", "βœ…"}
19
+ return "".join(char if char not in emoji.EMOJI_DATA or char in allowed_emojis else "" for char in text)
20
 
21
+ # βœ… Chat handler
22
  def chat_with_groq(message, history):
23
  messages = [{"role": "system", "content": "You are JAWERIA'SBOT πŸ€– – cheerful, emoji-savvy, and sleek."}]
24
  messages += history + [{"role": "user", "content": message}]
 
27
  "Authorization": f"Bearer {groq_key}",
28
  "Content-Type": "application/json"
29
  }
30
+
31
+ payload = {
32
+ "model": "llama3-70b-8192",
33
+ "messages": messages
34
+ }
35
+
36
+ response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
37
+ raw_reply = response.json()["choices"][0]["message"]["content"]
38
+ reply = filter_emojis(raw_reply)
39
 
40
  history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
41
  return "", history, history
42
 
43
+ # βœ… Transcription
44
  def transcribe_audio(audio_path):
45
+ if audio_path is None or not os.path.exists(audio_path):
46
+ return "⚠️ No audio input."
47
  try:
48
  temp_wav = f"{uuid.uuid4()}.wav"
49
  AudioSegment.from_file(audio_path).export(temp_wav, format="wav")
 
53
  except Exception as e:
54
  return f"❌ Transcription error: {e}"
55
 
56
+ # βœ… Save/load
57
  def save_session(history):
58
+ prompt = next((m["content"] for m in history if m["role"] == "user"), "chat")
59
+ title = re.sub(r"[^\w\s]", "", prompt).strip()
60
  title = " ".join(title.split()[:6])
61
+ timestamp = datetime.now().strftime("%b %d %Y %H-%M")
62
+ filename = f"{title} - {timestamp}.json"
63
  with open(filename, "w", encoding="utf-8") as f:
64
  json.dump(history, f, indent=2, ensure_ascii=False)
65
  return f"βœ… Saved `{filename[:-5]}`"
 
68
  return sorted([f[:-5] for f in os.listdir() if f.endswith(".json")])
69
 
70
  def load_chat(name):
71
+ filename = f"{name}.json"
72
  try:
73
+ with open(filename, "r", encoding="utf-8") as f:
74
  history = json.load(f)
75
  return history, history, f"βœ… Loaded `{name}`"
76
  except Exception as e:
77
  return [], [], f"❌ Load error: {e}"
78
 
79
+ # βœ… Gradio Interface
80
  with gr.Blocks(css="""
81
+ body { background-color: #111; font-family: 'Segoe UI', sans-serif; }
82
+ .gr-chatbot { background-color: #1e1e1e; border-radius: 10px; border: 1px solid #333; }
83
+ .gr-chatbot-message { background-color: #292929; color: #f1f1f1; border-radius: 10px; margin-bottom: 5px; }
84
  textarea, input[type='text'] {
85
+ background-color: #222; color: #fff; border-radius: 30px;
86
+ border: 1px solid #444; padding: 10px 15px; height: 48px;
87
  }
88
  .gr-button {
89
+ background-color: #333 !important; color: #fff !important;
90
+ border: 1px solid #444 !important; border-radius: 8px;
91
+ font-weight: bold; transition: background-color 0.2s;
92
  }
93
+ .gr-button:hover { background-color: #555 !important; cursor: pointer; }
94
+ .gr-dropdown { background-color: #222 !important; color: #fff !important; max-height: 200px; overflow-y: auto; }
95
+ h1, h2, h3, .gr-markdown { color: #f1f1f1 !important; text-align: center; }
96
  """) as demo:
97
 
98
  state = gr.State([])
99
+
100
+ gr.Markdown("# ✨ JAWERIA'SBOT πŸ€–")
101
+ gr.Markdown("Type or speak β€” your assistant listens and responds πŸ’¬")
102
+
103
+ chatbot = gr.Chatbot(label="JAWERIA'SBOT πŸ€–", height=400, type="messages")
104
+
105
  with gr.Row():
106
+ chat_input = gr.Textbox(placeholder="Type your message...", scale=8, show_label=False)
107
+ record_btn = gr.Button("πŸŽ™οΈ", scale=1)
108
+ upload_btn = gr.File(file_types=[".mp3", ".wav", ".m4a"], label="πŸ“ Upload", visible=False)
109
 
 
110
  send_btn = gr.Button("Send πŸš€")
111
 
112
+ recording = gr.Audio(type="filepath", visible=False)
113
+
114
  with gr.Row():
115
+ new_chat_btn = gr.Button("πŸ†• New")
116
  save_btn = gr.Button("πŸ’Ύ Save")
117
+ saved_dropdown = gr.Dropdown(label="πŸ“‚ Load Saved", choices=list_saved_files(), interactive=True)
118
  load_btn = gr.Button("πŸ“₯ Load")
119
 
120
  save_msg = gr.Markdown()
121
  load_msg = gr.Markdown()
122
 
123
+ # Event bindings
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  send_btn.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
125
  chat_input.submit(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
126
+ recording.change(transcribe_audio, inputs=recording, outputs=chat_input)
127
+ upload_btn.change(transcribe_audio, inputs=upload_btn, outputs=chat_input)
 
 
 
128
 
129
+ def toggle_recording(current):
130
+ return gr.update(visible=not current)
131
+
132
+ record_btn.click(lambda: gr.update(visible=True), None, recording)
133
+
134
+ new_chat_btn.click(fn=lambda: ("", [], []), outputs=[chat_input, chatbot, state])
135
+ save_btn.click(fn=save_session, inputs=[state], outputs=[save_msg])
136
+ save_btn.click(fn=list_saved_files, outputs=[saved_dropdown])
137
+ load_btn.click(fn=load_chat, inputs=[saved_dropdown], outputs=[chatbot, state, load_msg])
138
+ demo.load(fn=list_saved_files, outputs=[saved_dropdown])
139
 
140
  demo.launch()
141
+