Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,13 +6,16 @@ import whisper
|
|
6 |
import requests
|
7 |
import emoji
|
8 |
|
|
|
9 |
groq_key = os.getenv("GROQ_API_KEY")
|
10 |
whisper_model = whisper.load_model("base")
|
11 |
|
|
|
12 |
def filter_emojis(text):
|
13 |
-
|
14 |
-
return "".join(char if char not in emoji.EMOJI_DATA or char in
|
15 |
|
|
|
16 |
def chat_with_groq(message, history):
|
17 |
messages = [{"role": "system", "content": "You are JAWERIA'SBOT π€ β cheerful, emoji-savvy, and sleek."}]
|
18 |
messages += history + [{"role": "user", "content": message}]
|
@@ -21,24 +24,17 @@ def chat_with_groq(message, history):
|
|
21 |
"Authorization": f"Bearer {groq_key}",
|
22 |
"Content-Type": "application/json"
|
23 |
}
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
"messages": messages
|
28 |
-
}
|
29 |
-
|
30 |
-
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
|
31 |
-
if response.status_code != 200:
|
32 |
-
return "", history, f"β API Error {response.status_code}: {response.text}"
|
33 |
-
raw_reply = response.json()["choices"][0]["message"]["content"]
|
34 |
-
reply = filter_emojis(raw_reply)
|
35 |
|
36 |
history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
|
37 |
return "", history, history
|
38 |
|
|
|
39 |
def transcribe_audio(audio_path):
|
40 |
-
if audio_path
|
41 |
-
return "β οΈ No audio
|
42 |
try:
|
43 |
temp_wav = f"{uuid.uuid4()}.wav"
|
44 |
AudioSegment.from_file(audio_path).export(temp_wav, format="wav")
|
@@ -48,12 +44,11 @@ def transcribe_audio(audio_path):
|
|
48 |
except Exception as e:
|
49 |
return f"β Transcription error: {e}"
|
50 |
|
|
|
51 |
def save_session(history):
|
52 |
-
|
53 |
-
title = re.sub(r"[^\w\s]", "", prompt).strip()
|
54 |
title = " ".join(title.split()[:6])
|
55 |
-
|
56 |
-
filename = f"{title} - {timestamp}.json"
|
57 |
with open(filename, "w", encoding="utf-8") as f:
|
58 |
json.dump(history, f, indent=2, ensure_ascii=False)
|
59 |
return f"β
Saved `{filename[:-5]}`"
|
@@ -62,77 +57,69 @@ def list_saved_files():
|
|
62 |
return sorted([f[:-5] for f in os.listdir() if f.endswith(".json")])
|
63 |
|
64 |
def load_chat(name):
|
65 |
-
filename = f"{name}.json"
|
66 |
try:
|
67 |
-
with open(
|
68 |
history = json.load(f)
|
69 |
return history, history, f"β
Loaded `{name}`"
|
70 |
except Exception as e:
|
71 |
return [], [], f"β Load error: {e}"
|
72 |
|
|
|
73 |
with gr.Blocks(css="""
|
74 |
-
body { background
|
75 |
-
.gr-chatbot { border: 1px solid #333; background-color: #1e1e1e; border-radius: 10px; }
|
76 |
-
.gr-chatbot-message { background-color: #292929; color: #f1f1f1; border-radius: 10px; margin-bottom: 5px; }
|
77 |
textarea, input[type='text'] {
|
78 |
-
background-color: #222; color:
|
79 |
-
border: 1px solid #444;
|
80 |
}
|
81 |
.gr-button {
|
82 |
-
background-color: #333 !important; color:
|
83 |
-
border: 1px solid #444
|
84 |
-
font-weight: bold; transition: background-color 0.2s;
|
85 |
}
|
86 |
-
.gr-button:hover { background
|
87 |
-
.gr-dropdown { background
|
88 |
-
|
89 |
-
input[type="file"] { color: white; }
|
90 |
-
#mic-dropdown { position: absolute; right: 20px; top: 8px; }
|
91 |
""") as demo:
|
92 |
|
93 |
state = gr.State([])
|
94 |
-
audio_file = gr.File(visible=False)
|
95 |
-
|
96 |
-
gr.Markdown("# β¨ JAWERIA'SBOT π€")
|
97 |
-
gr.Markdown("Speak or type β your assistant listens and replies with text and emojis π¬")
|
98 |
-
|
99 |
-
chatbot = gr.Chatbot(type="messages", height=400)
|
100 |
-
|
101 |
with gr.Row():
|
102 |
-
chat_input = gr.Textbox(
|
103 |
-
|
|
|
104 |
|
|
|
105 |
send_btn = gr.Button("Send π")
|
106 |
|
107 |
with gr.Row():
|
108 |
-
|
109 |
save_btn = gr.Button("πΎ Save")
|
110 |
-
|
111 |
load_btn = gr.Button("π₯ Load")
|
112 |
|
113 |
save_msg = gr.Markdown()
|
114 |
load_msg = gr.Markdown()
|
115 |
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
send_btn.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
|
118 |
chat_input.submit(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
-
|
121 |
-
if choice == "ποΈ Record Audio":
|
122 |
-
return gr.Audio(source="microphone", type="filepath", label="Recording... ποΈ", interactive=True, visible=True)
|
123 |
-
elif choice == "π Upload File":
|
124 |
-
return gr.Audio(source="upload", type="filepath", label="Upload Audio π", interactive=True, visible=True)
|
125 |
-
else:
|
126 |
-
return gr.update(visible=False)
|
127 |
-
|
128 |
-
mic_audio = gr.Audio(visible=False, type="filepath")
|
129 |
-
mic_menu.change(handle_mic_choice, inputs=mic_menu, outputs=mic_audio)
|
130 |
-
mic_audio.change(transcribe_audio, inputs=mic_audio, outputs=chat_input)
|
131 |
-
|
132 |
-
new_chat_btn.click(fn=lambda: ("", [], []), outputs=[chat_input, chatbot, state])
|
133 |
-
save_btn.click(fn=save_session, inputs=[state], outputs=[save_msg])
|
134 |
-
save_btn.click(fn=list_saved_files, outputs=[saved_dropdown])
|
135 |
-
load_btn.click(fn=load_chat, inputs=[saved_dropdown], outputs=[chatbot, state, load_msg])
|
136 |
-
demo.load(fn=list_saved_files, outputs=[saved_dropdown])
|
137 |
|
138 |
demo.launch()
|
|
|
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 |
"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 |
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 |
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()
|