Spaces:
Sleeping
Sleeping
create the file app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load a Hugging Face chat model
|
| 5 |
+
@st.cache_resource
|
| 6 |
+
def load_chatbot():
|
| 7 |
+
return pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1", max_new_tokens=200)
|
| 8 |
+
|
| 9 |
+
chatbot = load_chatbot()
|
| 10 |
+
|
| 11 |
+
# Streamlit UI
|
| 12 |
+
st.title("💬 Chat with Mistral (Open Source ChatGPT)")
|
| 13 |
+
st.markdown("Ask me anything!")
|
| 14 |
+
|
| 15 |
+
if "history" not in st.session_state:
|
| 16 |
+
st.session_state.history = []
|
| 17 |
+
|
| 18 |
+
user_input = st.text_input("Your message", "")
|
| 19 |
+
|
| 20 |
+
if user_input:
|
| 21 |
+
st.session_state.history.append({"role": "user", "content": user_input})
|
| 22 |
+
|
| 23 |
+
# Construct prompt
|
| 24 |
+
full_prompt = "\n".join(
|
| 25 |
+
[f"{m['role'].capitalize()}: {m['content']}" for m in st.session_state.history]
|
| 26 |
+
) + "\nAssistant:"
|
| 27 |
+
|
| 28 |
+
response = chatbot(full_prompt)[0]["generated_text"]
|
| 29 |
+
# Get only the new assistant response
|
| 30 |
+
reply = response[len(full_prompt):].strip()
|
| 31 |
+
st.session_state.history.append({"role": "assistant", "content": reply})
|
| 32 |
+
|
| 33 |
+
# Display chat history
|
| 34 |
+
for message in st.session_state.history:
|
| 35 |
+
st.markdown(f"**{message['role'].capitalize()}:** {message['content']}")
|