Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from groq import Groq
|
3 |
+
import base64
|
4 |
+
|
5 |
+
client = Groq(api_key = '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')
|
6 |
+
|
7 |
+
def encode_image(image_path) :
|
8 |
+
|
9 |
+
with open(image_path , 'rb') as image_file : return base64.b64encode(image_file.read()).decode('utf-8')
|
10 |
+
|
11 |
+
def analyze_image(image_path , prompt) :
|
12 |
+
|
13 |
+
base64_image = encode_image(image_path)
|
14 |
+
|
15 |
+
chat_completion = client.chat.completions.create(
|
16 |
+
messages = [
|
17 |
+
{
|
18 |
+
'role': 'user' ,
|
19 |
+
'content' : [
|
20 |
+
{
|
21 |
+
'type' : 'text' ,
|
22 |
+
'text' : prompt
|
23 |
+
} ,
|
24 |
+
{
|
25 |
+
'type' : 'image_url' ,
|
26 |
+
'image_url' : {'url' : f'data:image/jpeg;base64,{base64_image}'}
|
27 |
+
}
|
28 |
+
]
|
29 |
+
}
|
30 |
+
] ,
|
31 |
+
model = 'llama-3.2-90b-vision-preview'
|
32 |
+
)
|
33 |
+
|
34 |
+
return chat_completion.choices[0].message.content
|
35 |
+
demo = gr.Interface(
|
36 |
+
fn=analyze_image,
|
37 |
+
inputs=[
|
38 |
+
gr.Image(type = 'filepath') ,
|
39 |
+
gr.Textbox(label = 'Prompt' , placeholder = 'Describe the Image' )
|
40 |
+
],
|
41 |
+
outputs = 'text'
|
42 |
+
)
|
43 |
+
|
44 |
+
demo.launch()
|