Spaces:
Running
Running
Divyansh12
commited on
Commit
•
09e3194
1
Parent(s):
9518de8
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,70 @@
|
|
1 |
import streamlit as st
|
2 |
from llama_cpp import Llama
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
for
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
]
|
35 |
)
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
|
|
|
1 |
import streamlit as st
|
2 |
from llama_cpp import Llama
|
3 |
+
|
4 |
+
# Initialize the model
|
5 |
+
llm = Llama.from_pretrained(
|
6 |
+
repo_id="Divyansh12/check",
|
7 |
+
filename="unsloth.F16.gguf", # Ensure this matches your model file name
|
8 |
+
verbose=True,
|
9 |
+
n_ctx=32768,
|
10 |
+
n_threads=2,
|
11 |
+
chat_format="chatml"
|
12 |
+
)
|
13 |
+
|
14 |
+
# Define the function to get responses from the model
|
15 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
16 |
+
messages = [{"role": "system", "content": system_message}]
|
17 |
+
|
18 |
+
for user_message, assistant_message in history:
|
19 |
+
if user_message:
|
20 |
+
messages.append({"role": "user", "content": user_message})
|
21 |
+
if assistant_message:
|
22 |
+
messages.append({"role": "assistant", "content": assistant_message})
|
23 |
+
|
24 |
+
messages.append({"role": "user", "content": message})
|
25 |
+
|
26 |
+
response = ""
|
27 |
+
# Stream the response from the model
|
28 |
+
response_stream = llm.create_chat_completion(
|
29 |
+
messages=messages,
|
30 |
+
stream=True,
|
31 |
+
max_tokens=max_tokens,
|
32 |
+
temperature=temperature,
|
33 |
+
top_p=top_p
|
|
|
34 |
)
|
35 |
+
|
36 |
+
for chunk in response_stream:
|
37 |
+
if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]:
|
38 |
+
response += chunk['choices'][0]["delta"]["content"]
|
39 |
+
yield response
|
40 |
+
|
41 |
+
# Streamlit UI
|
42 |
+
st.title("Chatbot Application")
|
43 |
+
st.write("### Interact with the chatbot!")
|
44 |
+
|
45 |
+
# User input fields
|
46 |
+
system_message = st.text_input("System Message", value="You are a friendly Chatbot.")
|
47 |
+
user_message = st.text_area("Your Message:")
|
48 |
+
max_tokens = st.slider("Max New Tokens", min_value=1, max_value=2048, value=512)
|
49 |
+
temperature = st.slider("Temperature", min_value=0.1, max_value=4.0, value=0.7)
|
50 |
+
top_p = st.slider("Top-p (Nucleus Sampling)", min_value=0.1, max_value=1.0, value=0.95)
|
51 |
+
|
52 |
+
# Chat history
|
53 |
+
if 'history' not in st.session_state:
|
54 |
+
st.session_state.history = []
|
55 |
+
|
56 |
+
if st.button("Send"):
|
57 |
+
# Get the response from the model
|
58 |
+
response = respond(user_message, st.session_state.history, system_message, max_tokens, temperature, top_p)
|
59 |
+
|
60 |
+
# Add user message and model response to history
|
61 |
+
st.session_state.history.append((user_message, response))
|
62 |
+
|
63 |
+
# Display the chat history
|
64 |
+
st.write("### Chat History")
|
65 |
+
for user_msg, assistant_msg in st.session_state.history:
|
66 |
+
st.write(f"**User:** {user_msg}")
|
67 |
+
st.write(f"**Assistant:** {assistant_msg}")
|
68 |
+
|
69 |
|
70 |
|