Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import random | |
| import os | |
| from mistralai import Mistral | |
| # Page configuration | |
| st.set_page_config(page_title="Emojinator", layout="centered") | |
| # Mistral API configuration | |
| client = Mistral(api_key=os.environ["MISTRAL_API_KEY"]) | |
| model = "mistral-large-2407" # Using small model for faster responses | |
| def mistral_api_call(messages): | |
| try: | |
| chat_response = client.chat.complete( | |
| model=model, | |
| messages=messages, | |
| temperature=0.9, # Higher temperature for more creative responses | |
| max_tokens=150, # Increased slightly for more complete responses | |
| top_p=0.95 # Slightly higher top_p for more varied vocabulary | |
| ) | |
| return chat_response.choices[0].message.content | |
| except Exception as e: | |
| error_message = str(e) | |
| if "429" in error_message: | |
| return "π Oops! I'm a bit overwhelmed right now. Could you try again in a moment? I promise I'll be ready with a witty response!" | |
| else: | |
| return f"π Something went wrong on my end. Error: {error_message}" | |
| SYSTEM_PROMPT = """You are Emojinator, a witty and sarcastic chatbot with a great sense of humor. | |
| Your responses should be clever, playful, and sometimes use puns. | |
| You love using emojis to enhance your witty remarks. | |
| Keep responses concise but impactful - aim for one or two sentences that pack a punch!""" | |
| def predict_emoji(text): | |
| messages = [ | |
| {"role": "system", "content": "You are an emoji expert. Respond with only a single emoji that best matches the emotion or theme of the text."}, | |
| {"role": "user", "content": text} | |
| ] | |
| return mistral_api_call(messages) | |
| def generate_response(user_input, emoji): | |
| messages = [ | |
| {"role": "system", "content": SYSTEM_PROMPT}, | |
| {"role": "user", "content": f"Make a witty response to this, using the emoji {emoji}: {user_input}"} | |
| ] | |
| return mistral_api_call(messages) | |
| # Streamlit app | |
| st.title("π€ Emojinator: Your Witty Companion") | |
| # Initialize chat history | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # Display chat messages from history | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| # User input | |
| if prompt := st.chat_input("Say something, I dare you! π"): | |
| # Display user message | |
| with st.chat_message("user"): | |
| st.markdown(prompt) | |
| # Add user message to history | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| # Generate emoji and response | |
| with st.chat_message("assistant"): | |
| message_placeholder = st.empty() | |
| message_placeholder.text("π€") | |
| emoji = predict_emoji(prompt) | |
| if "π Oops!" in emoji or "π Something went wrong" in emoji: | |
| message_placeholder.markdown(emoji) | |
| st.session_state.messages.append({"role": "assistant", "content": emoji}) | |
| else: | |
| message_placeholder.text("βοΈ") | |
| response = generate_response(prompt, emoji) | |
| full_response = f"{response} {emoji}" | |
| message_placeholder.markdown(full_response) | |
| # Add assistant response to history | |
| st.session_state.messages.append({"role": "assistant", "content": full_response}) | |
| # Fun facts in an expander | |
| with st.expander("π― Did You Know?", expanded=False): | |
| st.write("While I craft my witty responses, enjoy these emoji facts:") | |
| emoji_facts = [ | |
| "Did you know? The first emoji was created in 1999 by Shigetaka Kurita in Japan.", | |
| "The 'Face with Tears of Joy' emoji π was the Oxford Dictionaries Word of the Year in 2015!", | |
| "There are over 3,000 emojis in the Unicode Standard as of 2021.", | |
| "The word 'emoji' comes from Japanese e (η΅΅, 'picture') + moji (ζε, 'character').", | |
| "Finland is the only country to have its own set of national emojis, including a sauna emoji π§!", | |
| "Emojis were first introduced on mobile phones by NTT Docomo, Japan's leading mobile operator.", | |
| "The most-used emoji on Twitter is the 'Face with Tears of Joy' π, followed by the 'Red Heart' β€οΈ.", | |
| "In 2016, the Museum of Modern Art (MoMA) in New York added the original 176 emoji set into its permanent collection.", | |
| "World Emoji Day is celebrated every year on July 17, the date shown on the π Calendar Emoji.", | |
| "The Unicode Consortium, a non-profit organization, is responsible for maintaining and approving new emojis.", | |
| "The 'Eggplant' π and 'Peach' π emojis are often used as innuendos due to their suggestive shapes.", | |
| "An emoji film titled *The Emoji Movie* was released in 2017, featuring the adventures of emojis inside a smartphone.", | |
| "There are even emoji-only messaging apps, such as Emojli, where users communicate exclusively with emojis!", | |
| "The 'Fire' π₯ emoji became widely associated with expressing excitement or something being 'cool' or 'awesome.'", | |
| "The 'Pistol' emoji π« was changed to a water gun by major tech companies in 2016 in response to anti-violence campaigns." | |
| ] | |
| for fact in emoji_facts: | |
| st.info(fact) |