Commit
·
d6bbfd2
1
Parent(s):
e4968b3
vision and text
Browse files- app.py +96 -46
- requirements.txt +7 -2
app.py
CHANGED
@@ -2,7 +2,11 @@ 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:
|
@@ -12,21 +16,37 @@ if not API_KEY:
|
|
12 |
# Initialize the client with Together AI provider
|
13 |
@st.cache_resource
|
14 |
def get_client():
|
15 |
-
return InferenceClient(
|
16 |
-
|
17 |
-
|
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 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
except Exception as e:
|
29 |
-
return f"Error
|
30 |
|
31 |
def generate_response(
|
32 |
message: str,
|
@@ -37,51 +57,80 @@ def generate_response(
|
|
37 |
top_p: float,
|
38 |
files=None
|
39 |
) -> Iterator[str]:
|
40 |
-
"""Generate streaming response from the model"""
|
41 |
client = get_client()
|
42 |
-
|
43 |
-
# Process file if uploaded
|
44 |
-
# Process multiple files if uploaded
|
45 |
-
all_content = ""
|
46 |
-
if files:
|
47 |
-
file_contents = [process_file(file) for file in files]
|
48 |
-
all_content = "\n\n".join([
|
49 |
-
f"File {i+1} content:\n{content}"
|
50 |
-
for i, content in enumerate(file_contents)
|
51 |
-
])
|
52 |
-
|
53 |
-
if all_content:
|
54 |
-
message = f"{all_content}\n\nUser message:\n{message}"
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
messages = [{"role": "system", "content": system_message}]
|
57 |
|
58 |
-
# Add
|
59 |
for user_msg, assistant_msg in history:
|
60 |
-
|
61 |
-
|
62 |
-
if assistant_msg:
|
63 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
64 |
-
|
65 |
-
# Add current message
|
66 |
-
messages.append({"role": "user", "content": message})
|
67 |
|
68 |
try:
|
69 |
-
|
70 |
-
model
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
for chunk in stream:
|
79 |
-
if chunk.choices and chunk.choices[0].delta
|
80 |
yield chunk.choices[0].delta.content
|
81 |
-
|
82 |
except Exception as e:
|
83 |
yield f"Error: {str(e)}"
|
84 |
-
|
85 |
def main():
|
86 |
st.set_page_config(page_title="DeepSeek Chat", page_icon="💭", layout="wide")
|
87 |
|
@@ -123,9 +172,10 @@ def main():
|
|
123 |
)
|
124 |
uploaded_file = st.file_uploader(
|
125 |
"Upload File (optional)",
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
129 |
)
|
130 |
|
131 |
# Display chat messages
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
from typing import Iterator
|
5 |
+
from PIL import Image
|
6 |
+
import pytesseract
|
7 |
+
from PyPDF2 import PdfReader
|
8 |
+
import base64
|
9 |
+
from together import Together
|
10 |
|
11 |
API_KEY = os.getenv("TOGETHER_API_KEY")
|
12 |
if not API_KEY:
|
|
|
16 |
# Initialize the client with Together AI provider
|
17 |
@st.cache_resource
|
18 |
def get_client():
|
19 |
+
#return InferenceClient(
|
20 |
+
# provider="together",
|
21 |
+
# api_key=API_KEY
|
22 |
+
#)
|
23 |
+
return Together(api_key=API_KEY) # Use Together.ai's official client
|
24 |
|
25 |
def process_file(file) -> str:
|
26 |
"""Process uploaded file and return its content"""
|
27 |
if file is None:
|
28 |
return ""
|
29 |
+
|
30 |
try:
|
31 |
+
# Handle PDF files
|
32 |
+
if file.type == "application/pdf":
|
33 |
+
text = ""
|
34 |
+
pdf_reader = PdfReader(file)
|
35 |
+
for page in pdf_reader.pages:
|
36 |
+
page_text = page.extract_text()
|
37 |
+
if page_text:
|
38 |
+
text += page_text + "\n"
|
39 |
+
return text
|
40 |
+
|
41 |
+
# Handle image files
|
42 |
+
elif file.type.startswith("image/"):
|
43 |
+
return base64.b64encode(file.getvalue()).decode("utf-8")
|
44 |
+
|
45 |
+
# Handle text files
|
46 |
+
else:
|
47 |
+
return file.getvalue().decode('utf-8')
|
48 |
except Exception as e:
|
49 |
+
return f"Error processing file: {str(e)}"
|
50 |
|
51 |
def generate_response(
|
52 |
message: str,
|
|
|
57 |
top_p: float,
|
58 |
files=None
|
59 |
) -> Iterator[str]:
|
|
|
60 |
client = get_client()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
has_images = False
|
63 |
+
content_blocks = []
|
64 |
+
image_content = None # To store image data
|
65 |
+
image_mime_type = None # To store MIME type
|
66 |
+
|
67 |
+
if files:
|
68 |
+
for file in files:
|
69 |
+
content = process_file(file)
|
70 |
+
if file.type.startswith("image/"):
|
71 |
+
has_images = True
|
72 |
+
image_content = content # Already base64 encoded
|
73 |
+
image_mime_type = file.type # Store MIME type
|
74 |
+
else:
|
75 |
+
content_blocks.append({
|
76 |
+
"type": "text",
|
77 |
+
"text": f"File content:\n{content}"
|
78 |
+
})
|
79 |
+
|
80 |
+
# Build messages
|
81 |
messages = [{"role": "system", "content": system_message}]
|
82 |
|
83 |
+
# Add history
|
84 |
for user_msg, assistant_msg in history:
|
85 |
+
messages.append({"role": "user", "content": user_msg})
|
86 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
try:
|
89 |
+
if has_images:
|
90 |
+
# Vision model request
|
91 |
+
vision_messages = [{
|
92 |
+
"role": "user",
|
93 |
+
"content": [
|
94 |
+
{"type": "text", "text": message},
|
95 |
+
{
|
96 |
+
"type": "image_url",
|
97 |
+
"image_url": {
|
98 |
+
"url": f"data:{image_mime_type};base64,{image_content}",
|
99 |
+
},
|
100 |
+
},
|
101 |
+
]
|
102 |
+
}]
|
103 |
+
|
104 |
+
stream = client.chat.completions.create(
|
105 |
+
model="meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo",
|
106 |
+
messages=vision_messages,
|
107 |
+
stream=True,
|
108 |
+
)
|
109 |
+
|
110 |
+
else:
|
111 |
+
# Text-only model request
|
112 |
+
current_message = {
|
113 |
+
"role": "user",
|
114 |
+
"content": [{"type": "text", "text": message}] + content_blocks
|
115 |
+
}
|
116 |
+
messages.append(current_message)
|
117 |
+
|
118 |
+
stream = client.chat.completions.create(
|
119 |
+
model="deepseek-ai/DeepSeek-R1",
|
120 |
+
messages=messages,
|
121 |
+
max_tokens=max_tokens,
|
122 |
+
temperature=temperature,
|
123 |
+
top_p=top_p,
|
124 |
+
stream=True
|
125 |
+
)
|
126 |
+
|
127 |
+
# Stream response
|
128 |
for chunk in stream:
|
129 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
130 |
yield chunk.choices[0].delta.content
|
131 |
+
|
132 |
except Exception as e:
|
133 |
yield f"Error: {str(e)}"
|
|
|
134 |
def main():
|
135 |
st.set_page_config(page_title="DeepSeek Chat", page_icon="💭", layout="wide")
|
136 |
|
|
|
172 |
)
|
173 |
uploaded_file = st.file_uploader(
|
174 |
"Upload File (optional)",
|
175 |
+
type=['txt', 'py', 'md', 'swift', 'java', 'js', 'ts', 'rb', 'go',
|
176 |
+
'php', 'c', 'cpp', 'h', 'hpp', 'cs', 'html', 'css', 'kt', 'svelte',
|
177 |
+
'pdf', 'png', 'jpg', 'jpeg'], # Added file types
|
178 |
+
accept_multiple_files=True
|
179 |
)
|
180 |
|
181 |
# Display chat messages
|
requirements.txt
CHANGED
@@ -1,2 +1,7 @@
|
|
1 |
-
huggingface_hub==0.
|
2 |
-
streamlit
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.29.0
|
2 |
+
streamlit
|
3 |
+
pytesseract
|
4 |
+
PyPDF2
|
5 |
+
Pillow
|
6 |
+
pytesseract
|
7 |
+
together
|