Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
MIT License

Copyright (c) 2024 Kamalakar Satapathi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
tbd
126 changes: 63 additions & 63 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
import streamlit as st
import os
from PIL import Image
import google.generativeai as genai
import base64
import requests
import json
# from io import BytesIO

st.title('Image Captioning and Tagging')
# Setting up the title of the application
st.title('Art Style Explorer')

uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
# Input for the API key
API_KEY = st.text_input("Enter your DeepInfra API Key:", type="password")

API_KEY = st.text_input("Enter your API Key:      Get your Google Studio API key from [here](https://makersuite.google.com/app/apikey)", type="password")
if uploaded_file is not None:
if st.button('Upload'):
if API_KEY.strip() == '':
st.error('Enter a valid API key')
else:
file_path = os.path.join("temp", uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.getvalue())
img = Image.open(file_path)
try:
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel('gemini-pro-vision')
caption = model.generate_content(["Write a caption for the image in english",img])
tags=model.generate_content(["Generate 5 hash tags for the image in a line in english",img])
st.image(img, caption=f"Caption: {caption.text}")
st.write(f"Tags: {tags.text}")
except Exception as e:
error_msg = str(e)
if "API_KEY_INVALID" in error_msg:
st.error("Invalid API Key. Please enter a valid API Key.")
else:
st.error(f"Failed to configure API due to {error_msg}")
footer="""
<style>
a:link, a:visited {
color: blue;
text-decoration: dotted; /* Remove underline */
}
# Input for the image URL
image_url = st.text_input("Enter the URL of the image:")

a:hover, a:active {
color: skyblue;
}
.footer .p{
font-size:10px;
}
# Input for the prompt sent to the API
prompt = st.text_input("What would you like the model to tell you from this image?")

/* Footer */
.footer {
position:fixed;
left: 0;
bottom: 0;
width: 100%;
height:10%;
font-size:15px;
color: white;
text-align: center;
padding: 10px 0;
}
.footer p{
font-size:20px;
}
# File uploader allows the user to upload an image
# uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])

.footer a:hover {
color: white;
}
</style>
#d ef encode_image_to_base64(image):
# """Encode image to base64."""
# buffered = BytesIO()
# image.save(buffered, format="JPEG")
# return base64.b64encode(buffered.getvalue()).decode("utf-8")

<div class="footer">
<p>Developed with ❤ by <a href="https://www.linkedin.com/in/sgvkamalakar" target="_blank">sgvkamalakar</a></p>
</div>
"""
st.markdown(footer,unsafe_allow_html=True)
def call_deepinfra_api(image_url, prompt, api_key):
"""Send the image URL and prompt to DeepInfra for inference."""
url = "https://api.deepinfra.com/v1/openai/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": "llava-hf/llava-1.5-7b-hf",
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": image_url
}
},
{
"type": "text",
"text": prompt
}
]
}
]
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
return response.json()
else:
st.error(f"API request failed with status code: {response.status_code} and message: {response.text}")
return None

if image_url and API_KEY and prompt:
if st.button('Analyze Image'):
try:
result = call_deepinfra_api(image_url, API_KEY, prompt)
if result:
# Convert the dictionary to JSON formatted string and display it
json_result = json.dumps(result, indent=2) # Beautify the JSON response
st.json(json_result) # Use st.json to render the JSON in the UI
except Exception as e:
st.error(f"Failed to process image due to: {str(e)}")
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
google-generativeai==0.3.1
streamlit==1.29.0
streamlit==1.29.0