Spaces:
Sleeping
Sleeping
Upload 9 files
Browse files- __pycache__/config.cpython-312.pyc +0 -0
- app.py +41 -0
- helpers/__init__.py +0 -0
- helpers/__pycache__/__init__.cpython-312.pyc +0 -0
- helpers/__pycache__/image_helper.cpython-312.pyc +0 -0
- helpers/__pycache__/llm_helper.cpython-312.pyc +0 -0
- helpers/image_helper.py +24 -0
- helpers/llm_helper.py +22 -0
- requirements.txt +3 -0
__pycache__/config.cpython-312.pyc
ADDED
|
Binary file (786 Bytes). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from config import Config
|
| 3 |
+
from helpers.image_helper import create_temp_file
|
| 4 |
+
from helpers.llm_helper import analyze_image_file, stream_parser
|
| 5 |
+
|
| 6 |
+
page_title = Config.PAGE_TITLE
|
| 7 |
+
|
| 8 |
+
# configures page settings
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title=page_title,
|
| 11 |
+
initial_sidebar_state="expanded",
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# page title
|
| 15 |
+
st.title(page_title)
|
| 16 |
+
|
| 17 |
+
st.markdown("Select an image file to analyze.")
|
| 18 |
+
|
| 19 |
+
# displays file upload widget
|
| 20 |
+
uploaded_file = st.file_uploader("Choose image file", type=['png', 'jpg', 'jpeg'] )
|
| 21 |
+
|
| 22 |
+
# sets up sidebar nav widgets
|
| 23 |
+
#with st.sidebar:
|
| 24 |
+
# creates selectbox to pick the model we would like to use
|
| 25 |
+
image_model = 'llava:7b'
|
| 26 |
+
|
| 27 |
+
if chat_input := st.chat_input("What would you like to ask?"):
|
| 28 |
+
if uploaded_file is None:
|
| 29 |
+
st.error('You must select an image file to analyze!')
|
| 30 |
+
st.stop()
|
| 31 |
+
|
| 32 |
+
# Color formatting example https://docs.streamlit.io/library/api-reference/text/st.markdown
|
| 33 |
+
with st.status(":red[Processing image file. DON'T LEAVE THIS PAGE WHILE IMAGE FILE IS BEING ANALYZED...]", expanded=True) as status:
|
| 34 |
+
st.write(":orange[Analyzing Image File...]")
|
| 35 |
+
|
| 36 |
+
# creates the audio file
|
| 37 |
+
stream = analyze_image_file(uploaded_file, model=image_model, user_prompt=chat_input)
|
| 38 |
+
|
| 39 |
+
stream_output = st.write_stream(stream_parser(stream))
|
| 40 |
+
|
| 41 |
+
st.write(":green[Done analyzing image file]")
|
helpers/__init__.py
ADDED
|
File without changes
|
helpers/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (132 Bytes). View file
|
|
|
helpers/__pycache__/image_helper.cpython-312.pyc
ADDED
|
Binary file (1.19 kB). View file
|
|
|
helpers/__pycache__/llm_helper.cpython-312.pyc
ADDED
|
Binary file (835 Bytes). View file
|
|
|
helpers/image_helper.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import io
|
| 3 |
+
import tempfile
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
def create_temp_file(text_file):
|
| 7 |
+
# create a local tempfile of file that was selected to be uploaded
|
| 8 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp:
|
| 9 |
+
tmp.write(text_file.getvalue())
|
| 10 |
+
tmp_path = tmp.name # Save the path where the tempfile has been written
|
| 11 |
+
|
| 12 |
+
return tmp_path
|
| 13 |
+
|
| 14 |
+
def get_image_bytes(image_file):
|
| 15 |
+
# Open the image file
|
| 16 |
+
image_path = image_file # Replace with the path to your image file
|
| 17 |
+
image = Image.open(image_path)
|
| 18 |
+
|
| 19 |
+
# Convert the image to bytes
|
| 20 |
+
with io.BytesIO() as output:
|
| 21 |
+
image.save(output, format="png") # Change the format as needed (e.g., JPEG, PNG)
|
| 22 |
+
image_bytes = output.getvalue()
|
| 23 |
+
|
| 24 |
+
return image_bytes
|
helpers/llm_helper.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ollama import generate
|
| 2 |
+
from config import Config
|
| 3 |
+
from helpers.image_helper import get_image_bytes
|
| 4 |
+
|
| 5 |
+
system_prompt = Config.SYSTEM_PROMPT
|
| 6 |
+
|
| 7 |
+
def analyze_image_file(image_file, model, user_prompt):
|
| 8 |
+
# gets image bytes using helper function
|
| 9 |
+
image_bytes = get_image_bytes(image_file)
|
| 10 |
+
|
| 11 |
+
# calls the llava model using Ollama SDK
|
| 12 |
+
stream = generate(model=model,
|
| 13 |
+
prompt=user_prompt,
|
| 14 |
+
images=[image_bytes],
|
| 15 |
+
stream=True)
|
| 16 |
+
|
| 17 |
+
return stream
|
| 18 |
+
|
| 19 |
+
# handles stream response back from LLM
|
| 20 |
+
def stream_parser(stream):
|
| 21 |
+
for chunk in stream:
|
| 22 |
+
yield chunk['response']
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
python-dotenv
|
| 3 |
+
ollama
|