Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
from io import BytesIO
|
| 3 |
+
import base64
|
| 4 |
+
import os
|
| 5 |
+
from groq import Groq
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 9 |
+
client = Groq(api_key=api_key)
|
| 10 |
+
|
| 11 |
+
def encode_image(image_path):
|
| 12 |
+
"""Encode the image to base64."""
|
| 13 |
+
try:
|
| 14 |
+
# Open the image file
|
| 15 |
+
image = Image.open(image_path).convert("RGB")
|
| 16 |
+
|
| 17 |
+
# Resize the image to a height of 512 while maintaining the aspect ratio
|
| 18 |
+
base_height = 512
|
| 19 |
+
h_percent = (base_height / float(image.size[1]))
|
| 20 |
+
w_size = int((float(image.size[0]) * float(h_percent)))
|
| 21 |
+
image = image.resize((w_size, base_height), Image.LANCZOS)
|
| 22 |
+
|
| 23 |
+
# Convert the image to a byte stream
|
| 24 |
+
buffered = BytesIO()
|
| 25 |
+
image.save(buffered, format="JPEG")
|
| 26 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 27 |
+
|
| 28 |
+
return img_str
|
| 29 |
+
except FileNotFoundError:
|
| 30 |
+
print(f"Error: The file {image_path} was not found.")
|
| 31 |
+
return None
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error: {e}")
|
| 34 |
+
return None
|
| 35 |
+
|
| 36 |
+
def feifeichat(image):
|
| 37 |
+
try:
|
| 38 |
+
if image is None:
|
| 39 |
+
yield "Please upload a photo"
|
| 40 |
+
return
|
| 41 |
+
|
| 42 |
+
base64_image = encode_image(image)
|
| 43 |
+
if not base64_image:
|
| 44 |
+
yield "Error processing image"
|
| 45 |
+
return
|
| 46 |
+
|
| 47 |
+
# Groq requires a different format for image messages
|
| 48 |
+
messages = [
|
| 49 |
+
{
|
| 50 |
+
"role": "user",
|
| 51 |
+
"content": [
|
| 52 |
+
{"type": "text", "text": "Please provide a detailed description of this photo and state everything in plain text"},
|
| 53 |
+
{
|
| 54 |
+
"type": "image_url",
|
| 55 |
+
"image_url": {
|
| 56 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
]
|
| 60 |
+
}
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
response = client.chat.completions.create(
|
| 64 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 65 |
+
messages=messages,
|
| 66 |
+
stream=True
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
partial_message = ""
|
| 70 |
+
for chunk in response:
|
| 71 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 72 |
+
partial_message += chunk.choices[0].delta.content
|
| 73 |
+
yield partial_message
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"Error: {e}")
|
| 77 |
+
yield "An error occurred while processing your request"
|
| 78 |
+
|
| 79 |
+
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("Image To Flux Prompt")
|
| 81 |
+
with gr.Tab(label="Image To Flux Prompt"):
|
| 82 |
+
input_img = gr.Image(label="Input Picture", height=320, type="filepath")
|
| 83 |
+
output_text = gr.Textbox(label="Flux Prompt")
|
| 84 |
+
submit_btn = gr.Button(value="Submit")
|
| 85 |
+
|
| 86 |
+
submit_btn.click(
|
| 87 |
+
fn=feifeichat,
|
| 88 |
+
inputs=input_img,
|
| 89 |
+
outputs=output_text
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
demo.launch()
|