general_chatbot / app.py
JaweriaGenAI's picture
Update app.py
ab9f19a verified
raw
history blame
5.18 kB
import os
os.system("pip install -q git+https://github.com/openai/whisper.git")
import gradio as gr
import os, json, re, uuid
from datetime import datetime
from pydub import AudioSegment
import whisper
import requests
import emoji
# Load keys and models
groq_key = os.getenv("GROQ_API_KEY")
whisper_model = whisper.load_model("base")
# Emoji filter for assistant replies
def filter_emojis(text):
allowed = {"😊", "πŸ˜„", "πŸ‘", "πŸ€–", "✨", "πŸŽ‰", "πŸ’¬", "πŸ™Œ", "😎", "πŸ“’", "🧠", "βœ…"}
return "".join(char if char not in emoji.EMOJI_DATA or char in allowed else "" for char in text)
# Chat function using Groq API
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}
res = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
reply = filter_emojis(res.json()["choices"][0]["message"]["content"])
history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
return "", history, history
# Transcribe audio from mic or file
def transcribe_audio(audio_path):
if not audio_path or not os.path.exists(audio_path):
return "⚠️ No audio available."
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 chat sessions
def save_session(history):
title = re.sub(r"[^\w\s]", "", next((m["content"] for m in history if m["role"] == "user"), "chat")).strip()
title = " ".join(title.split()[:6])
filename = f"{title} - {datetime.now().strftime('%b %d %Y %H-%M')}.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):
try:
with open(f"{name}.json", "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}"
# Interface
with gr.Blocks(css="""
body { background: #111; color: white; font-family: 'Segoe UI'; }
textarea, input[type='text'] {
background-color: #222; color: white; border-radius: 30px;
padding: 10px 15px; border: 1px solid #444; height: 48px;
}
.gr-button {
background-color: #333 !important; color: white !important;
border: 1px solid #444; border-radius: 10px; font-weight: bold;
}
.gr-button:hover { background: #555 !important; }
.gr-dropdown { background: #222; color: white; max-height: 200px; overflow-y: auto; }
.gr-chatbot-message { background: #292929 !important; color: white; border-radius: 10px; }
""") as demo:
state = gr.State([])
with gr.Row():
chat_input = gr.Textbox(placeholder="Type or speak...", label=None, scale=9)
record_btn = gr.Button("πŸŽ™οΈ Record", scale=1)
upload_btn = gr.Audio(source="upload", type="filepath", visible=False)
chatbot = gr.Chatbot(label="JAWERIA'SBOT πŸ€–", height=400)
send_btn = gr.Button("Send πŸš€")
with gr.Row():
new_btn = gr.Button("πŸ†• New")
save_btn = gr.Button("πŸ’Ύ Save")
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()
recording = gr.Audio(source="microphone", type="filepath", visible=False)
def toggle_recording(is_recording):
return not is_recording, gr.update(visible=not is_recording)
def start_record(): return gr.update(visible=True)
def stop_record(path): return transcribe_audio(path)
record_state = gr.State(False)
record_btn.click(start_record, outputs=recording).then(
fn=lambda: True, outputs=record_state)
recording.change(fn=stop_record, inputs=recording, outputs=chat_input)
upload_btn.change(fn=transcribe_audio, inputs=upload_btn, outputs=chat_input)
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])
new_btn.click(lambda: ("", [], []), outputs=[chat_input, chatbot, state])
save_btn.click(save_session, inputs=[state], outputs=[save_msg])
save_btn.click(list_saved_files, outputs=[dropdown])
load_btn.click(load_chat, inputs=[dropdown], outputs=[chatbot, state, load_msg])
demo.load(list_saved_files, outputs=[dropdown])
gr.Markdown("<center><small>Made with 🀍 by Jaweria</small></center>")
demo.launch()