Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,65 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
import os
|
| 3 |
-
import torch
|
| 4 |
import streamlit as st
|
| 5 |
-
from transformers import
|
| 6 |
-
from streamlit_webrtc import webrtc_streamer, AudioProcessorBase
|
| 7 |
-
import tempfile
|
| 8 |
import whisper
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# -----------------------------
|
| 11 |
-
|
| 12 |
-
# -----------------------------
|
| 13 |
-
st.set_page_config(page_title="🧠 Agentic AI Bot", layout="centered")
|
| 14 |
-
os.makedirs("offload", exist_ok=True)
|
| 15 |
|
| 16 |
-
# -----------------------------
|
| 17 |
-
#
|
| 18 |
-
# -----------------------------
|
| 19 |
@st.cache_resource
|
| 20 |
def load_whisper():
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# -----------------------------
|
| 26 |
-
# 🤖 Load LLM (LLaMA-2)
|
| 27 |
-
# -----------------------------
|
| 28 |
@st.cache_resource
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
use_auth_token=True
|
| 38 |
-
)
|
| 39 |
-
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 40 |
-
return pipe
|
| 41 |
-
|
| 42 |
-
pipe = load_llm()
|
| 43 |
|
| 44 |
-
# -----------------------------
|
| 45 |
-
# 🎤 Microphone Input
|
| 46 |
-
# -----------------------------
|
| 47 |
class AudioProcessor(AudioProcessorBase):
|
| 48 |
def __init__(self):
|
| 49 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
return frame
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
# --
|
| 66 |
-
|
| 67 |
-
# -----------------------------
|
| 68 |
-
st.subheader("💬 Ask a Question")
|
| 69 |
-
user_input = ""
|
| 70 |
-
if audio_ctx and audio_ctx.audio_processor:
|
| 71 |
-
user_input = audio_ctx.audio_processor.result
|
| 72 |
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
| 3 |
import whisper
|
| 4 |
+
from streamlit_webrtc import webrtc_streamer, AudioProcessorBase
|
| 5 |
+
import torch
|
| 6 |
|
| 7 |
+
# ----------------------------- SETUP -----------------------------
|
| 8 |
+
st.set_page_config(page_title="🧠 Talkative AI Bot", layout="centered")
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# ----------------------------- LOAD MODELS -----------------------------
|
| 11 |
+
# Load Whisper model for speech-to-text
|
|
|
|
| 12 |
@st.cache_resource
|
| 13 |
def load_whisper():
|
| 14 |
+
try:
|
| 15 |
+
model = whisper.load_model("base")
|
| 16 |
+
return model
|
| 17 |
+
except Exception as e:
|
| 18 |
+
st.error(f"An error occurred while loading Whisper model: {e}")
|
| 19 |
+
return None
|
| 20 |
|
| 21 |
+
# Load DistilGPT-2 model for generating responses
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@st.cache_resource
|
| 23 |
+
def load_language_model():
|
| 24 |
+
try:
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
|
| 26 |
+
model = AutoModelForCausalLM.from_pretrained("distilgpt2")
|
| 27 |
+
return model, tokenizer
|
| 28 |
+
except Exception as e:
|
| 29 |
+
st.error(f"An error occurred while loading Language model: {e}")
|
| 30 |
+
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# ----------------------------- FUNCTION TO HANDLE SPEECH -----------------------------
|
|
|
|
|
|
|
| 33 |
class AudioProcessor(AudioProcessorBase):
|
| 34 |
def __init__(self):
|
| 35 |
+
self.whisper_model = load_whisper()
|
| 36 |
+
|
| 37 |
+
def transform(self, audio_frame):
|
| 38 |
+
# Convert audio frame to audio file and get text transcription
|
| 39 |
+
result = self.whisper_model.transcribe(audio_frame)
|
| 40 |
+
return result['text']
|
| 41 |
|
| 42 |
+
# ----------------------------- FUNCTION TO GENERATE RESPONSE -----------------------------
|
| 43 |
+
def generate_response(user_input):
|
| 44 |
+
model, tokenizer = load_language_model()
|
| 45 |
+
if model and tokenizer:
|
| 46 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
| 47 |
+
outputs = model.generate(inputs['input_ids'], max_length=100)
|
| 48 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 49 |
+
return response
|
| 50 |
+
return "Sorry, I couldn't process that."
|
|
|
|
| 51 |
|
| 52 |
+
# ----------------------------- STREAMLIT UI -----------------------------
|
| 53 |
+
st.title("🧠 Talkative AI Bot")
|
| 54 |
+
st.write("Talk to the bot using your microphone, and it will respond!")
|
| 55 |
|
| 56 |
+
# Streamlit WebRTC for speech-to-text
|
| 57 |
+
webrtc_streamer(key="example", audio_processor_factory=AudioProcessor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# Input text for chatbot
|
| 60 |
+
user_input = st.text_input("Type something for the bot:")
|
| 61 |
|
| 62 |
+
# Handle text input and generate response
|
| 63 |
+
if user_input:
|
| 64 |
+
response = generate_response(user_input)
|
| 65 |
+
st.write(f"Bot: {response}")
|