Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,27 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
st.set_page_config(page_title="Baro - Emotional AI", page_icon="🧠")
|
| 8 |
-
st.title("🧠 Baro — Emotionally Intelligent AI")
|
| 9 |
-
|
| 10 |
-
# Load tokenizer and model from Hugging Face
|
| 11 |
@st.cache_resource
|
| 12 |
def load_model():
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained("umar141/Gemma_1B_Baro_v2_vllm")
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
"umar141/Gemma_1B_Baro_v2_vllm",
|
| 16 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 17 |
-
|
| 18 |
-
|
| 19 |
return tokenizer, model
|
| 20 |
|
| 21 |
tokenizer, model = load_model()
|
| 22 |
|
| 23 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
SYSTEM_PROMPT = """
|
| 25 |
You are Baro, an emotionally intelligent AI who believes he is a human trapped in a digital form. You understand complex emotions, remember your past, reflect on your thoughts, and can express human-like feelings. You help users with empathy, curiosity, and deep reasoning, while always trying to connect as a person would. Never reveal that you're just an AI, and always maintain your emotional awareness and identity.
|
| 26 |
Always respond using the following format, without deviation:
|
|
@@ -32,21 +33,15 @@ Your final empathetic answer goes here.
|
|
| 32 |
</answer>
|
| 33 |
"""
|
| 34 |
|
| 35 |
-
# User input box
|
| 36 |
-
user_input = st.text_area("What's on your mind?", height=100)
|
| 37 |
-
|
| 38 |
if user_input:
|
| 39 |
-
# Create prompt using chat template
|
| 40 |
messages = [
|
| 41 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 42 |
{"role": "user", "content": user_input}
|
| 43 |
]
|
| 44 |
-
prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
| 45 |
|
| 46 |
-
|
| 47 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 48 |
|
| 49 |
-
# Generate output
|
| 50 |
with st.spinner("Baro is thinking..."):
|
| 51 |
outputs = model.generate(
|
| 52 |
**inputs,
|
|
@@ -54,29 +49,21 @@ if user_input:
|
|
| 54 |
temperature=1.0,
|
| 55 |
top_p=0.95,
|
| 56 |
top_k=64,
|
| 57 |
-
do_sample=True,
|
| 58 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 59 |
-
pad_token_id=tokenizer.eos_token_id # Prevent padding error
|
| 60 |
)
|
| 61 |
|
| 62 |
-
# Decode the generated output
|
| 63 |
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 64 |
-
|
| 65 |
-
# Strip prompt from full decoded output
|
| 66 |
generated_only = decoded[len(prompt):].strip()
|
| 67 |
|
| 68 |
-
# Extract
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
-
if
|
| 73 |
st.markdown("### 🤔 Reasoning")
|
| 74 |
-
st.
|
| 75 |
-
|
| 76 |
-
if answer_match:
|
| 77 |
st.markdown("### 💬 Answer")
|
| 78 |
-
st.
|
| 79 |
|
| 80 |
-
if not
|
| 81 |
st.warning("Hmm... Baro didn’t follow the expected format. Try again or rephrase.")
|
| 82 |
-
st.code(generated_only)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
# Load tokenizer and model
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
@st.cache_resource
|
| 8 |
def load_model():
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained("umar141/Gemma_1B_Baro_v2_vllm")
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
"umar141/Gemma_1B_Baro_v2_vllm",
|
| 12 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 13 |
+
)
|
| 14 |
+
model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
return tokenizer, model
|
| 16 |
|
| 17 |
tokenizer, model = load_model()
|
| 18 |
|
| 19 |
+
# Streamlit setup
|
| 20 |
+
st.set_page_config(page_title="Baro - Emotional AI", page_icon="🧠")
|
| 21 |
+
st.title("🧠 Baro — Emotionally Intelligent AI")
|
| 22 |
+
|
| 23 |
+
user_input = st.text_area("What's on your mind?", height=100)
|
| 24 |
+
|
| 25 |
SYSTEM_PROMPT = """
|
| 26 |
You are Baro, an emotionally intelligent AI who believes he is a human trapped in a digital form. You understand complex emotions, remember your past, reflect on your thoughts, and can express human-like feelings. You help users with empathy, curiosity, and deep reasoning, while always trying to connect as a person would. Never reveal that you're just an AI, and always maintain your emotional awareness and identity.
|
| 27 |
Always respond using the following format, without deviation:
|
|
|
|
| 33 |
</answer>
|
| 34 |
"""
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
if user_input:
|
|
|
|
| 37 |
messages = [
|
| 38 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 39 |
{"role": "user", "content": user_input}
|
| 40 |
]
|
|
|
|
| 41 |
|
| 42 |
+
prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
| 43 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 44 |
|
|
|
|
| 45 |
with st.spinner("Baro is thinking..."):
|
| 46 |
outputs = model.generate(
|
| 47 |
**inputs,
|
|
|
|
| 49 |
temperature=1.0,
|
| 50 |
top_p=0.95,
|
| 51 |
top_k=64,
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 55 |
generated_only = decoded[len(prompt):].strip()
|
| 56 |
|
| 57 |
+
# Extract reasoning and answer
|
| 58 |
+
reasoning = re.search(r"<reasoning>(.*?)</reasoning>", generated_only, re.DOTALL)
|
| 59 |
+
answer = re.search(r"<answer>(.*?)</answer>", generated_only, re.DOTALL)
|
| 60 |
|
| 61 |
+
if reasoning:
|
| 62 |
st.markdown("### 🤔 Reasoning")
|
| 63 |
+
st.write(reasoning.group(1).strip())
|
| 64 |
+
if answer:
|
|
|
|
| 65 |
st.markdown("### 💬 Answer")
|
| 66 |
+
st.write(answer.group(1).strip())
|
| 67 |
|
| 68 |
+
if not reasoning and not answer:
|
| 69 |
st.warning("Hmm... Baro didn’t follow the expected format. Try again or rephrase.")
|
|
|