Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import random
|
3 |
+
import os
|
4 |
+
from mistralai import Mistral
|
5 |
+
|
6 |
+
# Page configuration
|
7 |
+
st.set_page_config(page_title="Emojinator", layout="centered")
|
8 |
+
|
9 |
+
# Mistral API configuration
|
10 |
+
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
|
11 |
+
model = "mistral-large-2407" # Using small model for faster responses
|
12 |
+
|
13 |
+
def mistral_api_call(messages):
|
14 |
+
chat_response = client.chat.complete(
|
15 |
+
model=model,
|
16 |
+
messages=messages,
|
17 |
+
temperature=0.9, # Higher temperature for more creative responses
|
18 |
+
max_tokens=150, # Increased slightly for more complete responses
|
19 |
+
top_p=0.95 # Slightly higher top_p for more varied vocabulary
|
20 |
+
)
|
21 |
+
return chat_response.choices[0].message.content
|
22 |
+
|
23 |
+
SYSTEM_PROMPT = """You are Emojinator, a witty and sarcastic chatbot with a great sense of humor.
|
24 |
+
Your responses should be clever, playful, and sometimes use puns.
|
25 |
+
You love using emojis to enhance your witty remarks.
|
26 |
+
Keep responses concise but impactful - aim for one or two sentences that pack a punch!"""
|
27 |
+
|
28 |
+
@st.cache_data
|
29 |
+
def predict_emoji(text):
|
30 |
+
messages = [
|
31 |
+
{"role": "system", "content": "You are an emoji expert. Respond with only a single emoji that best matches the emotion or theme of the text."},
|
32 |
+
{"role": "user", "content": text}
|
33 |
+
]
|
34 |
+
return mistral_api_call(messages)
|
35 |
+
|
36 |
+
def generate_response(user_input, emoji):
|
37 |
+
messages = [
|
38 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
39 |
+
{"role": "user", "content": f"Make a witty response to this, using the emoji {emoji}: {user_input}"}
|
40 |
+
]
|
41 |
+
return mistral_api_call(messages)
|
42 |
+
|
43 |
+
# Streamlit app
|
44 |
+
st.title("π€ Emojinator: Your Witty Companion")
|
45 |
+
|
46 |
+
# Initialize chat history
|
47 |
+
if "messages" not in st.session_state:
|
48 |
+
st.session_state.messages = []
|
49 |
+
|
50 |
+
# Display chat messages from history
|
51 |
+
for message in st.session_state.messages:
|
52 |
+
with st.chat_message(message["role"]):
|
53 |
+
st.markdown(message["content"])
|
54 |
+
|
55 |
+
# User input
|
56 |
+
if prompt := st.chat_input("Say something, I dare you! π"):
|
57 |
+
# Display user message
|
58 |
+
with st.chat_message("user"):
|
59 |
+
st.markdown(prompt)
|
60 |
+
# Add user message to history
|
61 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
62 |
+
|
63 |
+
# Generate emoji and response
|
64 |
+
with st.chat_message("assistant"):
|
65 |
+
message_placeholder = st.empty()
|
66 |
+
message_placeholder.text("π€")
|
67 |
+
emoji = predict_emoji(prompt)
|
68 |
+
message_placeholder.text("βοΈ")
|
69 |
+
response = generate_response(prompt, emoji)
|
70 |
+
message_placeholder.markdown(f"{response} {emoji}")
|
71 |
+
# Add assistant response to history
|
72 |
+
st.session_state.messages.append({"role": "assistant", "content": f"{response} {emoji}"})
|
73 |
+
|
74 |
+
# Fun facts in an expander
|
75 |
+
with st.expander("π― Did You Know?", expanded=False):
|
76 |
+
st.write("While I craft my witty responses, enjoy these emoji facts:")
|
77 |
+
emoji_facts = [
|
78 |
+
"Did you know? The first emoji was created in 1999 by Shigetaka Kurita in Japan.",
|
79 |
+
"The 'Face with Tears of Joy' emoji π was the Oxford Dictionaries Word of the Year in 2015!",
|
80 |
+
"There are over 3,000 emojis in the Unicode Standard as of 2021.",
|
81 |
+
"The word 'emoji' comes from Japanese e (η΅΅, 'picture') + moji (ζε, 'character').",
|
82 |
+
"Finland is the only country to have its own set of national emojis, including a sauna emoji π§!",
|
83 |
+
"Emojis were first introduced on mobile phones by NTT Docomo, Japanβs leading mobile operator.",
|
84 |
+
"The most-used emoji on Twitter is the 'Face with Tears of Joy' π, followed by the 'Red Heart' β€οΈ.",
|
85 |
+
"In 2016, the Museum of Modern Art (MoMA) in New York added the original 176 emoji set into its permanent collection.",
|
86 |
+
"World Emoji Day is celebrated every year on July 17, the date shown on the π
Calendar Emoji.",
|
87 |
+
"The Unicode Consortium, a non-profit organization, is responsible for maintaining and approving new emojis.",
|
88 |
+
"The 'Eggplant' π and 'Peach' π emojis are often used as innuendos due to their suggestive shapes.",
|
89 |
+
"An emoji film titled *The Emoji Movie* was released in 2017, featuring the adventures of emojis inside a smartphone.",
|
90 |
+
"There are even emoji-only messaging apps, such as Emojli, where users communicate exclusively with emojis!",
|
91 |
+
"The 'Fire' π₯ emoji became widely associated with expressing excitement or something being 'cool' or 'awesome.'",
|
92 |
+
"The 'Pistol' emoji π« was changed to a water gun by major tech companies in 2016 in response to anti-violence campaigns."
|
93 |
+
]
|
94 |
+
|
95 |
+
for fact in emoji_facts:
|
96 |
+
st.info(fact)
|