Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import os
|
| 5 |
+
from mistralai import Mistral
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class ChatBot:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
|
| 11 |
+
self.history = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 12 |
+
|
| 13 |
+
def generate_response(self, prompt: str) -> str:
|
| 14 |
+
self.history.append({"role": "user", "content": prompt})
|
| 15 |
+
completion = self.client.agents.complete(
|
| 16 |
+
agent_id="ag:bfb4e4d9:20240809:coding-agent:b1d09feb",
|
| 17 |
+
messages=self.history,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
response = completion.choices[0].message.content
|
| 21 |
+
self.history.append({"role": "assistant", "content": response})
|
| 22 |
+
|
| 23 |
+
return response
|
| 24 |
+
|
| 25 |
+
def get_history(self) -> list:
|
| 26 |
+
return self.history
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Credit: Time
|
| 30 |
+
def current_year():
|
| 31 |
+
now = datetime.now()
|
| 32 |
+
return now.year
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
st.set_page_config(layout="wide")
|
| 36 |
+
st.title("Just chat! 🤖")
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
with st.sidebar:
|
| 40 |
+
with st.expander("Instruction Manual"):
|
| 41 |
+
st.markdown("""
|
| 42 |
+
## Mistral AI Agent 🤖 Chatbot
|
| 43 |
+
This Streamlit app allows you to chat with Mistral AI Coder Agent.
|
| 44 |
+
### How to Use:
|
| 45 |
+
1. **Input**: Type your prompt into the chat input box labeled "What is up?".
|
| 46 |
+
2. **Response**: The app will display a response from Mistral AI.
|
| 47 |
+
3. **Chat History**: Previous conversations will be shown on the app.
|
| 48 |
+
### Credits:
|
| 49 |
+
- **Developer**: [Yiqiao Yin](https://www.y-yin.io/) | [App URL](https://huggingface.co/spaces/eagle0504/gpt-4o-demo) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/)
|
| 50 |
+
Enjoy chatting with Mistral AI agent!
|
| 51 |
+
""")
|
| 52 |
+
|
| 53 |
+
# Example:
|
| 54 |
+
with st.expander("Examples"):
|
| 55 |
+
st.success("Example: Write a simple hello world flask app for me.")
|
| 56 |
+
st.success("Example: Write a program to simulate the value of pi.")
|
| 57 |
+
st.success("Example: Write a 3-layer neural network for me?")
|
| 58 |
+
|
| 59 |
+
# Add a button to clear the session state
|
| 60 |
+
if st.button("Clear Session"):
|
| 61 |
+
st.session_state.messages = []
|
| 62 |
+
st.experimental_rerun()
|
| 63 |
+
|
| 64 |
+
# Credit:
|
| 65 |
+
# current_year = current_year() # This will print the current year
|
| 66 |
+
# st.markdown(
|
| 67 |
+
# f"""
|
| 68 |
+
# <h6 style='text-align: left;'>Copyright © 2010-{current_year} Present Yiqiao Yin</h6>
|
| 69 |
+
# """,
|
| 70 |
+
# unsafe_allow_html=True,
|
| 71 |
+
# )
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Initialize chat history
|
| 75 |
+
if "messages" not in st.session_state:
|
| 76 |
+
st.session_state.messages = []
|
| 77 |
+
|
| 78 |
+
# Ensure messages are a list of dictionaries
|
| 79 |
+
if not isinstance(st.session_state.messages, list):
|
| 80 |
+
st.session_state.messages = []
|
| 81 |
+
if not all(isinstance(msg, dict) for msg in st.session_state.messages):
|
| 82 |
+
st.session_state.messages = []
|
| 83 |
+
|
| 84 |
+
# Display chat messages from history on app rerun
|
| 85 |
+
for message in st.session_state.messages:
|
| 86 |
+
with st.chat_message(message["role"]):
|
| 87 |
+
st.markdown(message["content"])
|
| 88 |
+
|
| 89 |
+
# React to user input
|
| 90 |
+
if prompt := st.chat_input("😉 Ask any question or feel free to use the examples provided in the left sidebar."):
|
| 91 |
+
|
| 92 |
+
# Display user message in chat message container
|
| 93 |
+
st.chat_message("user").markdown(prompt)
|
| 94 |
+
|
| 95 |
+
# Add user message to chat history
|
| 96 |
+
st.session_state.messages.append({"role": "system", "content": f"You are a helpful assistant. Year now is {current_year}"})
|
| 97 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 98 |
+
|
| 99 |
+
# API Call
|
| 100 |
+
bot = ChatBot()
|
| 101 |
+
bot.history = st.session_state.messages.copy() # Update history from messages
|
| 102 |
+
response = bot.generate_response(prompt)
|
| 103 |
+
|
| 104 |
+
# Display assistant response in chat message container
|
| 105 |
+
with st.chat_message("assistant"):
|
| 106 |
+
st.markdown(response)
|
| 107 |
+
|
| 108 |
+
# Add assistant response to chat history
|
| 109 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|