Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#imagetext-to-text
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import base64
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
client = InferenceClient('meta-llama/Llama-3.2-11B-Vision-Instruct')
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def imageDescription(image, prompt):
|
| 9 |
+
image_path="image.png"
|
| 10 |
+
image.save(image_path)
|
| 11 |
+
with open(image_path, "rb") as f:
|
| 12 |
+
base64_image = base64.b64encode(f.read()).decode("utf-8")
|
| 13 |
+
image_url = f"data:image/png;base64,{base64_image}"
|
| 14 |
+
output = client.chat.completions.create(messages=[
|
| 15 |
+
{
|
| 16 |
+
"role": "user",
|
| 17 |
+
"content": [
|
| 18 |
+
{
|
| 19 |
+
"type": "image_url",
|
| 20 |
+
"image_url": {"url": image_url},
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"type": "text",
|
| 24 |
+
"text": prompt,
|
| 25 |
+
},
|
| 26 |
+
],
|
| 27 |
+
},
|
| 28 |
+
],
|
| 29 |
+
)
|
| 30 |
+
return output.choices[0].message.content
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
with gr.Blocks(theme=gr.themes.Citrus()) as demo:
|
| 34 |
+
with gr.Row():
|
| 35 |
+
with gr.Column():
|
| 36 |
+
#an image input
|
| 37 |
+
image=gr.Image(type="pil", label="upload an immage")
|
| 38 |
+
prompt = gr.Textbox(label="What would you like to know about this picture?",scale=1)
|
| 39 |
+
describe_btn = gr.Button("Describe the image",scale=1)
|
| 40 |
+
output = gr.Textbox(label="Description",scale=1)
|
| 41 |
+
with gr.Column():
|
| 42 |
+
#sending two inputs to imageDescription function
|
| 43 |
+
describe_btn.click(fn=imageDescription, inputs=[image, prompt], outputs=output)
|
| 44 |
+
demo.launch(debug=True)
|