img-enhancer / app.py
kinely's picture
Update app.py
38de230 verified
import streamlit as st
from PIL import Image
from io import BytesIO
import requests
import os
from dotenv import load_dotenv
from groq import Groq
# Load environment variables from .env file
load_dotenv()
# Retrieve API URL and key from environment variables
API_URL = "https://vanceai.com/image-enhancer/?source=recomm" # Replace with actual VanceAI API URL if different
API_KEY = "915a4d81c8c13b0e2a27a165f26159c2" # Your VanceAI API key
# Streamlit UI
st.title("Image Enhancement Tool")
# Upload image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Open and display the uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image.", use_column_width=True)
if st.button("Enhance Image"):
# Convert image to bytes
buffered = BytesIO()
image.save(buffered, format="JPEG")
img_bytes = buffered.getvalue()
# Prepare the request
headers = {
"Authorization": f"Bearer {API_KEY}"
}
files = {
"image": ("image.jpg", img_bytes, "image/jpeg") # Adjust file name and type if needed
}
# Send the request to the image enhancement API
try:
response = requests.post(API_URL, headers=headers, files=files)
response.raise_for_status() # Raises HTTPError for bad responses
# Convert the response content back to an image
enhanced_image = Image.open(BytesIO(response.content))
st.image(enhanced_image, caption="Enhanced Image", use_column_width=True)
except requests.exceptions.RequestException as e:
st.error(f"An error occurred: {str(e)}")
# Load environment variables from .env file
load_dotenv()
# Groq API Key
GROQ_API_KEY = os.getenv("IMAGE_ENHANCEMENT_API_KEY")
client = Groq(
api_key=GROQ_API_KEY,
)
# Example usage
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Explain the importance of fast language models",
}
],
model="mixtral-8x7b-32768",
)
print(chat_completion.choices[0].message.content)