Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,54 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
-
from gtts import gTTS
|
| 4 |
-
import tempfile
|
| 5 |
-
import os
|
| 6 |
import torch
|
| 7 |
-
import
|
| 8 |
-
import soundfile as sf
|
| 9 |
-
import io
|
| 10 |
-
import logging
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
|
| 15 |
-
# Load model and tokenizer with error handling
|
| 16 |
@st.cache_resource
|
| 17 |
def load_model():
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
st.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
st.
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
audio_data = recognizer.record(source)
|
| 63 |
-
try:
|
| 64 |
-
return recognizer.recognize_google(audio_data)
|
| 65 |
-
except sr.UnknownValueError:
|
| 66 |
-
return "Sorry, I could not understand the audio."
|
| 67 |
-
except sr.RequestError:
|
| 68 |
-
return "Speech recognition service is unavailable."
|
| 69 |
-
|
| 70 |
-
# Input mode selection
|
| 71 |
-
mode = st.radio("Choose input mode:", ["Text", "Voice"])
|
| 72 |
-
|
| 73 |
-
user_input = ""
|
| 74 |
-
if mode == "Text":
|
| 75 |
-
user_input = st.text_input("Enter your query:")
|
| 76 |
-
else:
|
| 77 |
-
audio = st.file_uploader("Upload your voice (WAV format only)", type=["wav"])
|
| 78 |
-
if audio is not None:
|
| 79 |
-
audio_bytes = audio.read()
|
| 80 |
-
user_input = transcribe(audio_bytes)
|
| 81 |
-
st.write(f"You said: {user_input}")
|
| 82 |
-
|
| 83 |
-
# Run the assistant
|
| 84 |
-
if user_input:
|
| 85 |
-
with st.spinner("Thinking..."):
|
| 86 |
-
try:
|
| 87 |
-
result = pipe(user_input, max_new_tokens=200, temperature=0.7, do_sample=True)
|
| 88 |
-
response = result[0]['generated_text']
|
| 89 |
-
|
| 90 |
-
# Trim prompt from response if repeated
|
| 91 |
-
if response.lower().startswith(user_input.lower()):
|
| 92 |
-
response = response[len(user_input):].strip()
|
| 93 |
-
|
| 94 |
-
st.subheader("🤖 Assistant's Response:")
|
| 95 |
-
st.write(response)
|
| 96 |
-
|
| 97 |
-
# Speak response
|
| 98 |
-
audio_path = speak(response)
|
| 99 |
-
st.audio(audio_path, format="audio/mp3")
|
| 100 |
-
|
| 101 |
-
# Cleanup
|
| 102 |
-
os.remove(audio_path)
|
| 103 |
-
except Exception as e:
|
| 104 |
-
logging.error(f"Error while generating response: {str(e)}")
|
| 105 |
-
st.error("Sorry, there was an issue generating a response.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
+
import os
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# ✅ Must be first Streamlit command
|
| 7 |
+
st.set_page_config(page_title="🧠 Agentic AI Bot", layout="centered")
|
| 8 |
|
|
|
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
+
model_id = "meta-llama/Llama-2-7b-hf"
|
| 12 |
+
|
| 13 |
+
# Offload folder for Hugging Face Space
|
| 14 |
+
offload_dir = "/tmp/offload"
|
| 15 |
+
os.makedirs(offload_dir, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 18 |
+
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
model_id,
|
| 21 |
+
device_map="auto",
|
| 22 |
+
offload_folder=offload_dir,
|
| 23 |
+
low_cpu_mem_usage=True
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 27 |
+
return pipe
|
| 28 |
+
|
| 29 |
+
# Load the model
|
| 30 |
+
try:
|
| 31 |
+
with st.spinner("Loading model..."):
|
| 32 |
+
pipe = load_model()
|
| 33 |
+
except Exception as e:
|
| 34 |
+
st.error(f"An error occurred while loading the model: {e}")
|
| 35 |
+
st.stop()
|
| 36 |
+
|
| 37 |
+
# UI
|
| 38 |
+
st.title("🧠 Agentic AI Assistant")
|
| 39 |
+
st.markdown("Talk to your LLaMA 2-powered AI assistant.")
|
| 40 |
+
|
| 41 |
+
user_input = st.text_area("Enter your question:", height=150)
|
| 42 |
+
|
| 43 |
+
if st.button("Ask"):
|
| 44 |
+
if user_input.strip() == "":
|
| 45 |
+
st.warning("Please enter a message.")
|
| 46 |
+
else:
|
| 47 |
+
with st.spinner("Thinking..."):
|
| 48 |
+
try:
|
| 49 |
+
response = pipe(user_input, max_new_tokens=256, do_sample=True, temperature=0.7)[0]["generated_text"]
|
| 50 |
+
# Extract only new response portion
|
| 51 |
+
answer = response[len(user_input):].strip()
|
| 52 |
+
st.success(answer)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
st.error(f"An error occurred while generating a response: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|