Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,24 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
from datetime import datetime
|
4 |
from pydub import AudioSegment
|
5 |
import whisper
|
6 |
import requests
|
7 |
import emoji
|
8 |
|
9 |
-
# Load
|
10 |
groq_key = os.getenv("GROQ_API_KEY")
|
11 |
whisper_model = whisper.load_model("base")
|
12 |
|
13 |
-
#
|
14 |
def filter_emojis(text):
|
15 |
-
|
16 |
-
return "".join(char if char not in emoji.EMOJI_DATA or char in
|
17 |
|
18 |
-
# Chat
|
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 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
|
32 |
return "", history, history
|
33 |
|
34 |
-
#
|
35 |
def transcribe_audio(audio_path):
|
36 |
-
if
|
37 |
-
return "β οΈ No audio
|
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
|
48 |
def save_session(history):
|
49 |
-
|
|
|
50 |
title = " ".join(title.split()[:6])
|
51 |
-
|
|
|
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(
|
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;
|
|
|
|
|
70 |
textarea, input[type='text'] {
|
71 |
-
background-color: #222; color:
|
72 |
-
|
73 |
}
|
74 |
.gr-button {
|
75 |
-
background-color: #333 !important; color:
|
76 |
-
border: 1px solid #444; border-radius:
|
|
|
77 |
}
|
78 |
-
.gr-button:hover { background: #555 !important; }
|
79 |
-
.gr-dropdown { background: #222; color:
|
80 |
-
.gr-
|
81 |
""") as demo:
|
82 |
|
83 |
state = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
with gr.Row():
|
85 |
-
chat_input = gr.Textbox(placeholder="Type
|
86 |
-
record_btn = gr.Button("ποΈ
|
87 |
-
upload_btn = gr.
|
88 |
|
89 |
-
chatbot = gr.Chatbot(label="JAWERIA'SBOT π€", height=400)
|
90 |
send_btn = gr.Button("Send π")
|
91 |
|
|
|
|
|
92 |
with gr.Row():
|
93 |
-
|
94 |
save_btn = gr.Button("πΎ Save")
|
95 |
-
|
96 |
load_btn = gr.Button("π₯ Load")
|
97 |
|
98 |
save_msg = gr.Markdown()
|
99 |
load_msg = gr.Markdown()
|
100 |
|
101 |
-
|
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 |
-
|
118 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|