Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,78 @@
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
-
import
|
4 |
-
import
|
|
|
5 |
|
6 |
-
#
|
|
|
|
|
7 |
st.set_page_config(page_title="π§ Agentic AI Bot", layout="centered")
|
|
|
8 |
|
|
|
|
|
|
|
9 |
@st.cache_resource
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
# Offload folder for Hugging Face Space
|
14 |
-
offload_dir = "/tmp/offload"
|
15 |
-
os.makedirs(offload_dir, exist_ok=True)
|
16 |
|
17 |
-
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
model = AutoModelForCausalLM.from_pretrained(
|
20 |
model_id,
|
21 |
device_map="auto",
|
22 |
-
|
23 |
-
|
|
|
24 |
)
|
25 |
-
|
26 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
27 |
return pipe
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
import streamlit as st
|
5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
6 |
+
from streamlit_webrtc import webrtc_streamer, AudioProcessorBase
|
7 |
+
import tempfile
|
8 |
+
import whisper
|
9 |
|
10 |
+
# -----------------------------
|
11 |
+
# π SETUP
|
12 |
+
# -----------------------------
|
13 |
st.set_page_config(page_title="π§ Agentic AI Bot", layout="centered")
|
14 |
+
os.makedirs("offload", exist_ok=True)
|
15 |
|
16 |
+
# -----------------------------
|
17 |
+
# π§ Load Whisper Model (for mic)
|
18 |
+
# -----------------------------
|
19 |
@st.cache_resource
|
20 |
+
def load_whisper():
|
21 |
+
return whisper.load_model("base")
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
whisper_model = load_whisper()
|
24 |
|
25 |
+
# -----------------------------
|
26 |
+
# π€ Load LLM (LLaMA-2)
|
27 |
+
# -----------------------------
|
28 |
+
@st.cache_resource
|
29 |
+
def load_llm():
|
30 |
+
model_id = "meta-llama/Llama-2-7b-hf"
|
31 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=True)
|
32 |
model = AutoModelForCausalLM.from_pretrained(
|
33 |
model_id,
|
34 |
device_map="auto",
|
35 |
+
torch_dtype=torch.float16,
|
36 |
+
offload_folder="offload",
|
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.result = ""
|
50 |
+
|
51 |
+
def recv(self, frame):
|
52 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
53 |
+
f.write(frame.to_ndarray().tobytes())
|
54 |
+
audio_path = f.name
|
55 |
+
try:
|
56 |
+
text = whisper_model.transcribe(audio_path)["text"]
|
57 |
+
self.result = text
|
58 |
+
except:
|
59 |
+
self.result = "[Could not transcribe audio]"
|
60 |
+
return frame
|
61 |
+
|
62 |
+
st.subheader("ποΈ Speak to the Agent")
|
63 |
+
audio_ctx = webrtc_streamer(key="mic", audio_processor_factory=AudioProcessor)
|
64 |
+
|
65 |
+
# -----------------------------
|
66 |
+
# π§ Chat Interface
|
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 |
+
user_text = st.text_input("Or type your question here:", user_input)
|
74 |
+
|
75 |
+
if st.button("Ask") and user_text.strip():
|
76 |
+
with st.spinner("Thinking..."):
|
77 |
+
result = pipe(user_text, max_new_tokens=200, do_sample=True)[0]["generated_text"]
|
78 |
+
st.success(result)
|