general_chatbot / app.py
JaweriaGenAI's picture
Update app.py
d1b380c verified
import os
os.system("pip install -q git+https://github.com/openai/whisper.git")
import gradio as gr
import json, re, uuid
from datetime import datetime
from pydub import AudioSegment
import whisper
import requests
import emoji
# Load API key
groq_key = os.getenv("GROQ_API_KEY")
whisper_model = whisper.load_model("base")
# βœ… Safe emoji filtering
def filter_emojis(text):
allowed_emojis = {"😊", "πŸ˜„", "πŸ‘", "πŸ€–", "✨", "πŸŽ‰", "πŸ’¬", "πŸ™Œ", "😎", "πŸ“’", "🧠", "βœ…"}
return "".join(char if char not in emoji.EMOJI_DATA or char in allowed_emojis else "" for char in text)
# βœ… Chat handler
def chat_with_groq(message, history):
messages = [{"role": "system", "content": "You are JAWERIA'SBOT πŸ€– – cheerful, emoji-savvy, and sleek."}]
messages += history + [{"role": "user", "content": message}]
headers = {
"Authorization": f"Bearer {groq_key}",
"Content-Type": "application/json"
}
payload = {
"model": "llama3-70b-8192",
"messages": messages
}
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
raw_reply = response.json()["choices"][0]["message"]["content"]
reply = filter_emojis(raw_reply)
history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
return "", history, history
# βœ… Transcription
def transcribe_audio(audio_path):
if audio_path is None or not os.path.exists(audio_path):
return "⚠️ No audio input."
try:
temp_wav = f"{uuid.uuid4()}.wav"
AudioSegment.from_file(audio_path).export(temp_wav, format="wav")
result = whisper_model.transcribe(temp_wav)
os.remove(temp_wav)
return result["text"]
except Exception as e:
return f"❌ Transcription error: {e}"
# βœ… Save/load
def save_session(history):
prompt = next((m["content"] for m in history if m["role"] == "user"), "chat")
title = re.sub(r"[^\w\s]", "", prompt).strip()
title = " ".join(title.split()[:6])
timestamp = datetime.now().strftime("%b %d %Y %H-%M")
filename = f"{title} - {timestamp}.json"
with open(filename, "w", encoding="utf-8") as f:
json.dump(history, f, indent=2, ensure_ascii=False)
return f"βœ… Saved `{filename[:-5]}`"
def list_saved_files():
return sorted([f[:-5] for f in os.listdir() if f.endswith(".json")])
def load_chat(name):
filename = f"{name}.json"
try:
with open(filename, "r", encoding="utf-8") as f:
history = json.load(f)
return history, history, f"βœ… Loaded `{name}`"
except Exception as e:
return [], [], f"❌ Load error: {e}"
# βœ… Gradio Interface
with gr.Blocks(css="""
body { background-color: #111; font-family: 'Segoe UI', sans-serif; }
.gr-chatbot { background-color: #1e1e1e; border-radius: 10px; border: 1px solid #333; }
.gr-chatbot-message { background-color: #292929; color: #f1f1f1; border-radius: 10px; margin-bottom: 5px; }
textarea, input[type='text'] {
background-color: #222; color: #fff; border-radius: 30px;
border: 1px solid #444; padding: 10px 15px; height: 48px;
}
.gr-button {
background-color: #333 !important; color: #fff !important;
border: 1px solid #444 !important; border-radius: 8px;
font-weight: bold; transition: background-color 0.2s;
}
.gr-button:hover { background-color: #555 !important; cursor: pointer; }
.gr-dropdown { background-color: #222 !important; color: #fff !important; max-height: 200px; overflow-y: auto; }
h1, h2, h3, .gr-markdown { color: #f1f1f1 !important; text-align: center; }
""") as demo:
state = gr.State([])
gr.Markdown("# ✨ JAWERIA'SBOT πŸ€–")
gr.Markdown("Type or speak β€” your assistant listens and responds πŸ’¬")
chatbot = gr.Chatbot(label="JAWERIA'SBOT πŸ€–", height=400, type="messages")
with gr.Row():
chat_input = gr.Textbox(placeholder="Type your message...", scale=8, show_label=False)
record_btn = gr.Button("πŸŽ™οΈ", scale=1)
upload_btn = gr.File(file_types=[".mp3", ".wav", ".m4a"], label="πŸ“ Upload", visible=False)
send_btn = gr.Button("Send πŸš€")
recording = gr.Audio(type="filepath", visible=False)
with gr.Row():
new_chat_btn = gr.Button("πŸ†• New")
save_btn = gr.Button("πŸ’Ύ Save")
saved_dropdown = gr.Dropdown(label="πŸ“‚ Load Saved", choices=list_saved_files(), interactive=True)
load_btn = gr.Button("πŸ“₯ Load")
save_msg = gr.Markdown()
load_msg = gr.Markdown()
# Event bindings
send_btn.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
chat_input.submit(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
recording.change(transcribe_audio, inputs=recording, outputs=chat_input)
upload_btn.change(transcribe_audio, inputs=upload_btn, outputs=chat_input)
def toggle_recording(current):
return gr.update(visible=not current)
record_btn.click(lambda: gr.update(visible=True), None, recording)
new_chat_btn.click(fn=lambda: ("", [], []), outputs=[chat_input, chatbot, state])
save_btn.click(fn=save_session, inputs=[state], outputs=[save_msg])
save_btn.click(fn=list_saved_files, outputs=[saved_dropdown])
load_btn.click(fn=load_chat, inputs=[saved_dropdown], outputs=[chatbot, state, load_msg])
demo.load(fn=list_saved_files, outputs=[saved_dropdown])
demo.launch()