DrElaheJ commited on
Commit
39703f6
·
verified ·
1 Parent(s): 094e525

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
app.py CHANGED
@@ -1,44 +1,43 @@
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(share=True)
 
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)