Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,57 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
|
4 |
+
"""
|
5 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
+
"""
|
7 |
+
client = InferenceClient(model_id="mistralai/Codestral-22B-v0.1", token="your_token_here")
|
8 |
+
|
9 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
10 |
+
messages = [{"role": "system", "content": system_message}]
|
11 |
+
for user_msg, assistant_msg in history:
|
12 |
+
if user_msg:
|
13 |
+
messages.append({"role": "user", "content": user_msg})
|
14 |
+
if assistant_msg:
|
15 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
16 |
+
messages.append({"role": "user", "content": message})
|
17 |
+
|
18 |
+
response = ""
|
19 |
+
try:
|
20 |
+
for message in client.chat_completion(
|
21 |
+
messages=messages,
|
22 |
+
max_tokens=max_tokens,
|
23 |
+
stream=True,
|
24 |
+
temperature=temperature,
|
25 |
+
top_p=top_p,
|
26 |
+
):
|
27 |
+
token = message.choices[0].delta.content
|
28 |
+
response += token
|
29 |
+
yield response
|
30 |
+
except Exception as e:
|
31 |
+
yield f"Error: {e}"
|
32 |
+
|
33 |
+
# Streamlit interface
|
34 |
+
st.title("Chat with Codestral Model")
|
35 |
+
system_message = st.text_input("System message", value="You are an expert python coder with in depth knowledge of langchain.")
|
36 |
+
max_tokens = st.slider("Max new tokens", min_value=1, max_value=2048, value=2048, step=1)
|
37 |
+
temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.6, step=0.1)
|
38 |
+
top_p = st.slider("Top-p (nucleus sampling)", min_value=0.1, max_value=1.0, value=0.95, step=0.05)
|
39 |
+
|
40 |
+
history = []
|
41 |
+
|
42 |
+
if "history" not in st.session_state:
|
43 |
+
st.session_state.history = []
|
44 |
+
|
45 |
+
def get_response():
|
46 |
+
user_input = st.session_state.user_input
|
47 |
+
if user_input:
|
48 |
+
st.session_state.history.append((user_input, ""))
|
49 |
+
response_generator = respond(user_input, st.session_state.history, system_message, max_tokens, temperature, top_p)
|
50 |
+
response = ""
|
51 |
+
for r in response_generator:
|
52 |
+
response = r
|
53 |
+
st.session_state.history[-1] = (user_input, response)
|
54 |
+
|
55 |
+
st.text_area("Chat History", value="\n".join([f"User: {h[0]}\nAssistant: {h[1]}" for h in st.session_state.history]), height=300)
|
56 |
+
|
57 |
+
st.text_input("Your message:", key="user_input", on_change=get_response)
|