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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
from typing import Iterator
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
API_KEY = os.getenv("TOGETHER_API_KEY")
|
| 8 |
+
if not API_KEY:
|
| 9 |
+
raise ValueError("API key is missing! Make sure TOGETHER_API_KEY is set in the Secrets.")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Initialize the client with Together AI provider
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def get_client():
|
| 15 |
+
return InferenceClient(
|
| 16 |
+
provider="together",
|
| 17 |
+
api_key=API_KEY
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
def process_file(file) -> str:
|
| 21 |
+
"""Process uploaded file and return its content"""
|
| 22 |
+
if file is None:
|
| 23 |
+
return ""
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
content = file.getvalue().decode('utf-8')
|
| 27 |
+
return content
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Error reading file: {str(e)}"
|
| 30 |
+
|
| 31 |
+
def generate_response(
|
| 32 |
+
message: str,
|
| 33 |
+
history: list[tuple[str, str]],
|
| 34 |
+
system_message: str,
|
| 35 |
+
max_tokens: int,
|
| 36 |
+
temperature: float,
|
| 37 |
+
top_p: float,
|
| 38 |
+
file=None
|
| 39 |
+
) -> Iterator[str]:
|
| 40 |
+
"""Generate streaming response from the model"""
|
| 41 |
+
client = get_client()
|
| 42 |
+
|
| 43 |
+
# Process file if uploaded
|
| 44 |
+
file_content = process_file(file) if file else ""
|
| 45 |
+
|
| 46 |
+
# If there's file content, append it to the message
|
| 47 |
+
if file_content:
|
| 48 |
+
message = f"File content:\n{file_content}\n\nUser message:\n{message}"
|
| 49 |
+
|
| 50 |
+
messages = [{"role": "system", "content": system_message}]
|
| 51 |
+
|
| 52 |
+
# Add conversation history
|
| 53 |
+
for user_msg, assistant_msg in history:
|
| 54 |
+
if user_msg:
|
| 55 |
+
messages.append({"role": "user", "content": user_msg})
|
| 56 |
+
if assistant_msg:
|
| 57 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 58 |
+
|
| 59 |
+
# Add current message
|
| 60 |
+
messages.append({"role": "user", "content": message})
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
stream = client.chat.completions.create(
|
| 64 |
+
model="deepseek-ai/DeepSeek-R1",
|
| 65 |
+
messages=messages,
|
| 66 |
+
max_tokens=max_tokens,
|
| 67 |
+
stream=True,
|
| 68 |
+
temperature=temperature,
|
| 69 |
+
top_p=top_p,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
for chunk in stream:
|
| 73 |
+
if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content:
|
| 74 |
+
yield chunk.choices[0].delta.content
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
yield f"Error: {str(e)}"
|
| 78 |
+
|
| 79 |
+
def main():
|
| 80 |
+
st.set_page_config(page_title="DeepSeek Chat", page_icon="💭", layout="wide")
|
| 81 |
+
|
| 82 |
+
# Initialize session state for chat history
|
| 83 |
+
if "messages" not in st.session_state:
|
| 84 |
+
st.session_state.messages = []
|
| 85 |
+
|
| 86 |
+
st.title("DeepSeek Chat with File Upload")
|
| 87 |
+
st.markdown("Chat with DeepSeek AI model. You can optionally upload files for the model to analyze.")
|
| 88 |
+
|
| 89 |
+
# Sidebar for parameters
|
| 90 |
+
with st.sidebar:
|
| 91 |
+
st.header("Settings")
|
| 92 |
+
system_message = st.text_area(
|
| 93 |
+
"System Message",
|
| 94 |
+
value="You are a friendly Chatbot.",
|
| 95 |
+
height=100
|
| 96 |
+
)
|
| 97 |
+
max_tokens = st.slider(
|
| 98 |
+
"Max Tokens",
|
| 99 |
+
min_value=1,
|
| 100 |
+
max_value=8192,
|
| 101 |
+
value=8192,
|
| 102 |
+
step=1
|
| 103 |
+
)
|
| 104 |
+
temperature = st.slider(
|
| 105 |
+
"Temperature",
|
| 106 |
+
min_value=0.1,
|
| 107 |
+
max_value=4.0,
|
| 108 |
+
value=0.0,
|
| 109 |
+
step=0.1
|
| 110 |
+
)
|
| 111 |
+
top_p = st.slider(
|
| 112 |
+
"Top-p (nucleus sampling)",
|
| 113 |
+
min_value=0.1,
|
| 114 |
+
max_value=1.0,
|
| 115 |
+
value=0.95,
|
| 116 |
+
step=0.05
|
| 117 |
+
)
|
| 118 |
+
uploaded_file = st.file_uploader(
|
| 119 |
+
"Upload File (optional)",
|
| 120 |
+
type=['txt', 'py', 'md', 'csv']
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# Display chat messages
|
| 124 |
+
for message in st.session_state.messages:
|
| 125 |
+
with st.chat_message(message["role"]):
|
| 126 |
+
st.write(message["content"])
|
| 127 |
+
|
| 128 |
+
# Chat input
|
| 129 |
+
if prompt := st.chat_input("What would you like to know?"):
|
| 130 |
+
# Display user message
|
| 131 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 132 |
+
with st.chat_message("user"):
|
| 133 |
+
st.write(prompt)
|
| 134 |
+
|
| 135 |
+
# Generate and display assistant response
|
| 136 |
+
with st.chat_message("assistant"):
|
| 137 |
+
response_placeholder = st.empty()
|
| 138 |
+
full_response = ""
|
| 139 |
+
|
| 140 |
+
# Get message history for context
|
| 141 |
+
history = [(msg["content"], next_msg["content"])
|
| 142 |
+
for msg, next_msg in zip(st.session_state.messages[::2], st.session_state.messages[1::2])]
|
| 143 |
+
|
| 144 |
+
# Stream the response
|
| 145 |
+
for response_chunk in generate_response(
|
| 146 |
+
prompt,
|
| 147 |
+
history,
|
| 148 |
+
system_message,
|
| 149 |
+
max_tokens,
|
| 150 |
+
temperature,
|
| 151 |
+
top_p,
|
| 152 |
+
uploaded_file
|
| 153 |
+
):
|
| 154 |
+
full_response += response_chunk
|
| 155 |
+
print(full_response)
|
| 156 |
+
response_placeholder.markdown(full_response + "▌")
|
| 157 |
+
|
| 158 |
+
response_placeholder.markdown(full_response)
|
| 159 |
+
|
| 160 |
+
# Add assistant response to chat history
|
| 161 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 162 |
+
|
| 163 |
+
if __name__ == "__main__":
|
| 164 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.25.2
|
| 2 |
+
streamlit
|