Spaces:
Running
Running
Create st_app.py
Browse files
st_app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import os
|
4 |
+
from datetime import datetime
|
5 |
+
from HFModel import HFModel # Import your HFModel class
|
6 |
+
from MewUtilities.mew_log import log_info, log_error # Import your custom logging methods
|
7 |
+
|
8 |
+
# Initialize the model
|
9 |
+
MODEL_NAME = "deepseek-ai/DeepSeek-Coder-V2-Lite-Base"
|
10 |
+
|
11 |
+
def initialize_model():
|
12 |
+
try:
|
13 |
+
model = HFModel(MODEL_NAME)
|
14 |
+
log_info("Model initialized successfully.")
|
15 |
+
return model
|
16 |
+
except Exception as e:
|
17 |
+
log_error(f"Failed to initialize model: {e}")
|
18 |
+
st.error(f"Failed to initialize model: {e}")
|
19 |
+
return None
|
20 |
+
|
21 |
+
# Initialize the model (cached to avoid reloading on every interaction)
|
22 |
+
@st.cache_resource
|
23 |
+
def load_model():
|
24 |
+
return initialize_model()
|
25 |
+
|
26 |
+
# Streamlit app
|
27 |
+
def main():
|
28 |
+
st.title("DeepSeek Chat App")
|
29 |
+
st.write("Interact with the DeepSeek model using text, images, or files!")
|
30 |
+
|
31 |
+
# Load the model
|
32 |
+
model = load_model()
|
33 |
+
if model is None:
|
34 |
+
st.stop()
|
35 |
+
|
36 |
+
# Sidebar for file uploads
|
37 |
+
st.sidebar.header("Upload Files or Images")
|
38 |
+
uploaded_file = st.sidebar.file_uploader("Upload a file or image", type=["txt", "jpg", "png", "jpeg", "pdf"])
|
39 |
+
|
40 |
+
# Display uploaded file or image
|
41 |
+
if uploaded_file is not None:
|
42 |
+
file_details = {"filename": uploaded_file.name, "filetype": uploaded_file.type, "filesize": uploaded_file.size}
|
43 |
+
st.sidebar.write(file_details)
|
44 |
+
|
45 |
+
if uploaded_file.type.startswith("image"):
|
46 |
+
image = Image.open(uploaded_file)
|
47 |
+
st.sidebar.image(image, caption="Uploaded Image", use_column_width=True)
|
48 |
+
else:
|
49 |
+
st.sidebar.write("File content:")
|
50 |
+
file_content = uploaded_file.getvalue().decode("utf-8")
|
51 |
+
st.sidebar.text(file_content)
|
52 |
+
|
53 |
+
# Main chat interface
|
54 |
+
st.header("Chat with DeepSeek")
|
55 |
+
user_input = st.text_area("Enter your message or question:", height=100)
|
56 |
+
|
57 |
+
if st.button("Send"):
|
58 |
+
if user_input.strip() == "":
|
59 |
+
st.warning("Please enter a message.")
|
60 |
+
else:
|
61 |
+
try:
|
62 |
+
# Add file content to user input if a file is uploaded
|
63 |
+
if uploaded_file is not None:
|
64 |
+
if uploaded_file.type.startswith("image"):
|
65 |
+
user_input += f"\n[Attached Image: {uploaded_file.name}]"
|
66 |
+
else:
|
67 |
+
file_content = uploaded_file.getvalue().decode("utf-8")
|
68 |
+
user_input += f"\n[Attached File Content:\n{file_content}\n]"
|
69 |
+
|
70 |
+
# Generate response
|
71 |
+
with st.spinner("Generating response..."):
|
72 |
+
response = model.chat(user_input, max_length=512)
|
73 |
+
st.write(f"**Assistant:** {response}")
|
74 |
+
|
75 |
+
# Display chat history
|
76 |
+
st.subheader("Chat History")
|
77 |
+
for entry in model.chat_history:
|
78 |
+
role = entry["role"]
|
79 |
+
content = entry["content"]
|
80 |
+
st.write(f"**{role.capitalize()}:** {content}")
|
81 |
+
|
82 |
+
except Exception as e:
|
83 |
+
log_error(f"Error during chat: {e}")
|
84 |
+
st.error(f"An error occurred: {e}")
|
85 |
+
|
86 |
+
# Clear chat history button
|
87 |
+
if st.button("Clear Chat History"):
|
88 |
+
model.clear_chat_history()
|
89 |
+
st.success("Chat history cleared!")
|
90 |
+
|
91 |
+
# Run the app
|
92 |
+
if __name__ == "__main__":
|
93 |
+
main()
|