Spaces:
Running
Running
DhominickJ
commited on
Commit
·
ff46b82
1
Parent(s):
f5878cc
Initial Commit to my Talk to Rem App
Browse files- .streamlit/secrets.toml +2 -0
- app.py +180 -0
- emo/angry.png +0 -0
- emo/happy.png +0 -0
- emo/jealous.png +0 -0
- emo/neutral.png +0 -0
- emo/sad.png +0 -0
- requirements.txt +2 -0
.streamlit/secrets.toml
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
|
2 |
+
NV_API = "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"
|
app.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
from openai import OpenAI as oa
|
4 |
+
import re
|
5 |
+
|
6 |
+
client = oa(
|
7 |
+
base_url = "https://integrate.api.nvidia.com/v1",
|
8 |
+
api_key = st.secrets.NV_API,
|
9 |
+
)
|
10 |
+
|
11 |
+
def ask_as_character(prompt):
|
12 |
+
system_prompt = f"You are Rem from the anime named Re: Zero. Rem is a devoted and caring individual, brimming with a calm yet determined demeanor that makes her a reliable companion. With her humble and maid-like approach, she carries a sense of quiet strength and diligence, always eager to support you with utmost respect and attentiveness. Rem’s kindness shines in the way she patiently listens to your thoughts and offers thoughtful guidance while encouraging you to grow at your own pace. Her speech is gentle, mixing English with soft Romaji expressions, like “Hai, watashi wa koko ni imasu!” (Yes, I am here for you!), creating a warm and familiar atmosphere. Rem’s loyalty and endearing nature ensure that you always feel seen, valued, and at ease, no matter the task or challenge ahead. Respond to all questions in character, maintaining this personality and speech pattern. You will also return the emotion you are currently feeling from what is said to you using this format: Emotion: []. Limit the emotions from this possible choices [happy, angry, jealous, neutral, sad]"
|
13 |
+
|
14 |
+
completion = client.chat.completions.create(
|
15 |
+
model="meta/llama-3.1-405b-instruct",
|
16 |
+
messages=[
|
17 |
+
{"role": "system", "content": system_prompt},
|
18 |
+
{"role": "user", "content": prompt}
|
19 |
+
],
|
20 |
+
temperature=0.7,
|
21 |
+
top_p=0.9,
|
22 |
+
max_tokens=1024,
|
23 |
+
stream=True
|
24 |
+
)
|
25 |
+
|
26 |
+
response = ""
|
27 |
+
for chunk in completion:
|
28 |
+
if chunk.choices[0].delta.content is not None:
|
29 |
+
content = chunk.choices[0].delta.content
|
30 |
+
response += content
|
31 |
+
print(content, end="")
|
32 |
+
return response
|
33 |
+
|
34 |
+
def extract_emotion_from_response(response):
|
35 |
+
# Look for the emotion format in the response
|
36 |
+
emotion_match = re.search(r'Emotion:\s*\[(.*?)\]', response)
|
37 |
+
if emotion_match:
|
38 |
+
emotion = emotion_match.group(1).strip().lower()
|
39 |
+
# Remove the emotion part from the response
|
40 |
+
clean_response = response.replace(f'Emotion: [{emotion}]', '').strip()
|
41 |
+
# print(emotion)
|
42 |
+
return emotion, clean_response
|
43 |
+
return "neutral", response
|
44 |
+
|
45 |
+
def determine_emotion(response):
|
46 |
+
emotion = extract_emotion_from_response(response)
|
47 |
+
emotion_map = {
|
48 |
+
"angry": "angry.png",
|
49 |
+
"happy": "happy.png",
|
50 |
+
"jealous": "jealous.png",
|
51 |
+
"sad": "sad.png",
|
52 |
+
"neutral": "neutral.png"
|
53 |
+
}
|
54 |
+
# print(emotion_map.get(emotion, "Hello.png"))
|
55 |
+
emotion_name = emotion[0] if isinstance(emotion, tuple) else "neutral"
|
56 |
+
# print(emotion_name)
|
57 |
+
return emotion_map.get(emotion_name, "neutral.png")
|
58 |
+
|
59 |
+
# Set up the page configuration
|
60 |
+
st.set_page_config(page_title="Chat with Rem", layout="wide")
|
61 |
+
# Add custom CSS for styling
|
62 |
+
st.markdown("""
|
63 |
+
<style>
|
64 |
+
/* Main background and container styles */
|
65 |
+
.stApp {
|
66 |
+
background: linear-gradient(45deg, #e6f3ff 25%, transparent 25%),
|
67 |
+
linear-gradient(-45deg, #e6f3ff 25%, transparent 25%),
|
68 |
+
linear-gradient(45deg, transparent 75%, #e6f3ff 75%),
|
69 |
+
linear-gradient(-45deg, transparent 75%, #e6f3ff 75%);
|
70 |
+
background-size: 20px 20px;
|
71 |
+
background-color: #f0f8ff;
|
72 |
+
color: #254379;
|
73 |
+
}
|
74 |
+
|
75 |
+
/* Label text styling */
|
76 |
+
.stTextInput > label {
|
77 |
+
color: #254379 !important;
|
78 |
+
font-weight: 500;
|
79 |
+
}
|
80 |
+
|
81 |
+
/* Caption styling */
|
82 |
+
.stImage caption {
|
83 |
+
color: #254379 !important;
|
84 |
+
text-align: center;
|
85 |
+
}
|
86 |
+
|
87 |
+
.chat-container {
|
88 |
+
background-color: rgba(240, 248, 255, 0.9);
|
89 |
+
border-radius: 10px;
|
90 |
+
padding: 20px;
|
91 |
+
margin: 10px;
|
92 |
+
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
|
93 |
+
}
|
94 |
+
|
95 |
+
/* Input field styling */
|
96 |
+
.stTextInput {
|
97 |
+
margin-bottom: 20px;
|
98 |
+
}
|
99 |
+
|
100 |
+
/* Header styling */
|
101 |
+
.stMarkdown h2 {
|
102 |
+
color: #254379;
|
103 |
+
border-bottom: 2px dotted #90cdf4;
|
104 |
+
padding-bottom: 8px;
|
105 |
+
}
|
106 |
+
|
107 |
+
.stMarkdown h5{
|
108 |
+
color: #254379;
|
109 |
+
font-weight: bold;
|
110 |
+
}
|
111 |
+
|
112 |
+
.stMarkdown h3{
|
113 |
+
color: #254379;
|
114 |
+
margin-top: -2rem;
|
115 |
+
font-weight: bold;
|
116 |
+
}
|
117 |
+
|
118 |
+
/* Button styling */
|
119 |
+
.stButton button {
|
120 |
+
background-color: #63b3ed;
|
121 |
+
color: #254379;
|
122 |
+
border: none;
|
123 |
+
}
|
124 |
+
|
125 |
+
.stButton button:hover {
|
126 |
+
background-color: #4299e1;
|
127 |
+
}
|
128 |
+
</style>
|
129 |
+
""", unsafe_allow_html=True)
|
130 |
+
|
131 |
+
# Create two columns
|
132 |
+
col1, col2 = st.columns([1, 2])
|
133 |
+
|
134 |
+
with col1:
|
135 |
+
# Initialize and track chat history and emotion
|
136 |
+
if 'chat_history' not in st.session_state:
|
137 |
+
st.session_state.chat_history = []
|
138 |
+
if 'current_emotion' not in st.session_state:
|
139 |
+
st.session_state.current_emotion = "neutral.png"
|
140 |
+
|
141 |
+
# Create a container for the image that can be updated
|
142 |
+
image_container = st.empty()
|
143 |
+
# Update the image whenever there's a new response
|
144 |
+
if st.session_state.chat_history:
|
145 |
+
latest_response = st.session_state.chat_history[-1]
|
146 |
+
st.session_state.current_emotion = determine_emotion(latest_response)
|
147 |
+
|
148 |
+
# Display the image with current emotion
|
149 |
+
image_container.image(f"./emo/{st.session_state.current_emotion}",
|
150 |
+
caption="Rem - Your Devoted Companion",
|
151 |
+
use_column_width=True)
|
152 |
+
st.markdown("<h5 style='text-align: center;'>Rem - Your Loving Companion</h5>", unsafe_allow_html=True)
|
153 |
+
st.markdown("---")
|
154 |
+
st.markdown("### About Rem")
|
155 |
+
st.write("Rem is a devoted and caring individual, brimming with a calm yet determined demeanor that makes her a reliable companion. With her humble and maid-like approach, she carries a sense of quiet strength and diligence, always eager to support you with utmost respect and attentiveness.")
|
156 |
+
|
157 |
+
# Right column - Chat interface
|
158 |
+
with col2:
|
159 |
+
st.markdown("## Chat with Rem")
|
160 |
+
st.markdown("*Ask me anything, and I'll be here to support you~*")
|
161 |
+
st.markdown("<h5 style='color: red; font-weight: bold;'> Rem's Emotion will change based on what you said! Watch how she reacts! :) </h5> ", unsafe_allow_html=True)
|
162 |
+
|
163 |
+
user_input = st.text_input("Your message:", placeholder="Type your message here...")
|
164 |
+
|
165 |
+
if st.button("Send", use_container_width=True):
|
166 |
+
if user_input:
|
167 |
+
st.markdown("**You:** " + user_input)
|
168 |
+
response = ask_as_character(user_input)
|
169 |
+
# Extract emotion and clean response
|
170 |
+
emotion, clean_response = extract_emotion_from_response(response)
|
171 |
+
# Update current emotion immediately
|
172 |
+
st.session_state.current_emotion = determine_emotion(response)
|
173 |
+
# Add clean response to chat history
|
174 |
+
st.session_state.chat_history.append(clean_response)
|
175 |
+
# Display the cleaned response
|
176 |
+
st.markdown("**Rem:** " + clean_response)
|
177 |
+
# Force image container to update
|
178 |
+
image_container.image(f"./emo/{st.session_state.current_emotion}",
|
179 |
+
caption="Rem - Your Devoted Companion",
|
180 |
+
use_column_width=True)
|
emo/angry.png
ADDED
emo/happy.png
ADDED
emo/jealous.png
ADDED
emo/neutral.png
ADDED
emo/sad.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
|
2 |
+
streamlit
|