Spaces:
Sleeping
Sleeping
Update test_phi3.py
Browse files- test_phi3.py +60 -65
test_phi3.py
CHANGED
@@ -1,65 +1,60 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
st.
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
if st.
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
except Exception as e:
|
63 |
-
st.error(f"π¨ Error generating response: {e}")
|
64 |
-
|
65 |
-
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import streamlit as st
|
4 |
+
from llama_cpp import Llama
|
5 |
+
|
6 |
+
# β
Define model path inside Hugging Face Space
|
7 |
+
MODEL_PATH = "./Phi-3-mini-4k-instruct-q4.gguf"
|
8 |
+
MODEL_URL = "https://huggingface.co/YourModelRepo/Phi-3-mini-4k-instruct-q4.gguf"
|
9 |
+
|
10 |
+
# β
Check if model exists, otherwise download
|
11 |
+
if not os.path.exists(MODEL_PATH):
|
12 |
+
st.info("Downloading the model file. Please wait...")
|
13 |
+
with requests.get(MODEL_URL, stream=True) as response:
|
14 |
+
response.raise_for_status()
|
15 |
+
with open(MODEL_PATH, "wb") as f:
|
16 |
+
for chunk in response.iter_content(chunk_size=8192):
|
17 |
+
f.write(chunk)
|
18 |
+
st.success("Model downloaded successfully!")
|
19 |
+
|
20 |
+
# β
Load model in session
|
21 |
+
if "model" not in st.session_state:
|
22 |
+
st.session_state["model"] = Llama(model_path=MODEL_PATH, n_ctx=4096)
|
23 |
+
|
24 |
+
# Streamlit UI setup
|
25 |
+
st.set_page_config(page_title="Phi-3 Mini Chatbot", layout="centered")
|
26 |
+
st.title("π€ Phi-3 Mini Chatbot")
|
27 |
+
st.markdown("Enter a message and get responses from Phi-3 Mini!")
|
28 |
+
|
29 |
+
# Chat history
|
30 |
+
if "messages" not in st.session_state:
|
31 |
+
st.session_state["messages"] = []
|
32 |
+
|
33 |
+
# Display chat history
|
34 |
+
for message in st.session_state["messages"]:
|
35 |
+
role, text = message
|
36 |
+
if role == "user":
|
37 |
+
st.chat_message("user").write(text)
|
38 |
+
else:
|
39 |
+
st.chat_message("assistant").write(text)
|
40 |
+
|
41 |
+
# Input field for user message
|
42 |
+
user_input = st.text_input("Your Message:", "", key="user_input")
|
43 |
+
if st.button("Send") and user_input:
|
44 |
+
# Add user input to chat history
|
45 |
+
st.session_state["messages"].append(("user", user_input))
|
46 |
+
st.chat_message("user").write(user_input)
|
47 |
+
|
48 |
+
# Generate response
|
49 |
+
response = st.session_state["model"].create_completion(
|
50 |
+
prompt=user_input, max_tokens=1024, temperature=0.7, top_p=0.9
|
51 |
+
)["choices"][0]["text"].strip()
|
52 |
+
|
53 |
+
# Add model response to chat history
|
54 |
+
st.session_state["messages"].append(("assistant", response))
|
55 |
+
st.chat_message("assistant").write(response)
|
56 |
+
|
57 |
+
# Run the app with: streamlit run test_phi3.py
|
58 |
+
|
59 |
+
|
60 |
+
|
|
|
|
|
|
|
|
|
|