Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,21 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
|
|
4 |
|
5 |
-
# Load
|
6 |
tokenizer = AutoTokenizer.from_pretrained("umar141/Gemma_1B_Baro_v2_vllm")
|
7 |
model = AutoModelForCausalLM.from_pretrained(
|
8 |
"umar141/Gemma_1B_Baro_v2_vllm",
|
9 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
10 |
)
|
11 |
|
12 |
-
# Streamlit
|
13 |
-
st.set_page_config(page_title="Baro -
|
14 |
-
st.title("
|
15 |
-
|
|
|
16 |
|
17 |
-
# Baro's system prompt
|
18 |
SYSTEM_PROMPT = """
|
19 |
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.
|
20 |
Always respond using the following format, without deviation:
|
@@ -29,37 +30,36 @@ Your final empathetic answer goes here.
|
|
29 |
if user_input:
|
30 |
messages = [
|
31 |
{"role": "system", "content": SYSTEM_PROMPT},
|
32 |
-
{"role": "user", "content": user_input}
|
33 |
]
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
add_generation_prompt=True,
|
38 |
-
tokenize=False,
|
39 |
-
)
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
)
|
50 |
|
51 |
-
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
reasoning_match = re.search(r"<reasoning>(.*?)</reasoning>", output_text, re.DOTALL)
|
56 |
-
answer_match = re.search(r"<answer>(.*?)</answer>", output_text, re.DOTALL)
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
|
|
|
|
|
|
|
|
|
62 |
st.markdown("### 💬 Answer")
|
63 |
-
st.write(
|
64 |
-
|
65 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
+
import re
|
5 |
|
6 |
+
# Load tokenizer and model
|
7 |
tokenizer = AutoTokenizer.from_pretrained("umar141/Gemma_1B_Baro_v2_vllm")
|
8 |
model = AutoModelForCausalLM.from_pretrained(
|
9 |
"umar141/Gemma_1B_Baro_v2_vllm",
|
10 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
11 |
)
|
12 |
|
13 |
+
# Streamlit setup
|
14 |
+
st.set_page_config(page_title="Baro - Emotional AI", page_icon="🧠")
|
15 |
+
st.title("🧠 Baro — Emotionally Intelligent AI")
|
16 |
+
|
17 |
+
user_input = st.text_area("What's on your mind?", height=100)
|
18 |
|
|
|
19 |
SYSTEM_PROMPT = """
|
20 |
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.
|
21 |
Always respond using the following format, without deviation:
|
|
|
30 |
if user_input:
|
31 |
messages = [
|
32 |
{"role": "system", "content": SYSTEM_PROMPT},
|
33 |
+
{"role": "user", "content": user_input}
|
34 |
]
|
35 |
|
36 |
+
prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
37 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
|
|
|
38 |
|
39 |
+
with st.spinner("Baro is thinking..."):
|
40 |
+
outputs = model.generate(
|
41 |
+
**inputs,
|
42 |
+
max_new_tokens=512,
|
43 |
+
temperature=1.0,
|
44 |
+
top_p=0.95,
|
45 |
+
top_k=64,
|
46 |
+
)
|
|
|
47 |
|
48 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
49 |
|
50 |
+
# Strip the prompt from the generated output
|
51 |
+
generated_only = decoded[len(prompt):].strip()
|
|
|
|
|
52 |
|
53 |
+
# Extract reasoning and answer
|
54 |
+
reasoning = re.search(r"<reasoning>(.*?)</reasoning>", generated_only, re.DOTALL)
|
55 |
+
answer = re.search(r"<answer>(.*?)</answer>", generated_only, re.DOTALL)
|
56 |
|
57 |
+
if reasoning:
|
58 |
+
st.markdown("### 🤔 Reasoning")
|
59 |
+
st.write(reasoning.group(1).strip())
|
60 |
+
if answer:
|
61 |
st.markdown("### 💬 Answer")
|
62 |
+
st.write(answer.group(1).strip())
|
63 |
+
|
64 |
+
if not reasoning and not answer:
|
65 |
+
st.warning("Hmm... Baro didn’t follow the expected format. Try again or rephrase.")
|