DrElaheJ commited on
Commit
5c50f41
·
verified ·
1 Parent(s): c2295df

Create app.py

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