DrElaheJ commited on
Commit
9f5fa56
·
verified ·
1 Parent(s): 6af17d2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #imagetext-to-text
2
+ #huggingface spaces
3
+ import gradio as gr
4
+ import base64
5
+ from huggingface_hub import InferenceClient
6
+ client = InferenceClient('meta-llama/Llama-3.2-11B-Vision-Instruct')
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
+ with gr.Blocks(theme=gr.themes.Citrus()) as demo:
33
+ with gr.Row():
34
+ with gr.Column():
35
+ #an image input
36
+ image=gr.Image(type="pil", label="upload an immage")
37
+ prompt = gr.Textbox(label="What would you like to know about this picture?",scale=1)
38
+ describe_btn = gr.Button("Describe the image",scale=1)
39
+ output = gr.Textbox(label="Description",scale=1)
40
+ with gr.Column():
41
+ #sending two inputs to imageDescription function
42
+ describe_btn.click(fn=imageDescription, inputs=[image, prompt], outputs=output)
43
+ demo.launch(debug=True)