cutiee82 commited on
Commit
a7aae14
·
verified ·
1 Parent(s): 66ab5ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -23
app.py CHANGED
@@ -3,37 +3,33 @@ import os
3
  from io import BytesIO
4
 
5
  import gradio as gr
6
- import requests
7
 
8
- API_URL = "https://text.pollinations.ai/openai"
9
  PROMPT = os.environ.get("PROMPT", "Describe this image.")
10
 
 
 
11
  def image_to_base64(image):
12
  buf = BytesIO()
13
  image.save(buf, "JPEG")
14
- return base64.b64encode(buf.getvalue()).decode("utf-8")
 
15
 
16
  def caption(image):
17
- data = {
18
- "model": "openai-large",
19
- "messages": [
20
- {
21
- "role": "user",
22
- "content": [
23
- {"type": "text", "text": PROMPT},
24
- {
25
- "type": "image_url",
26
- "image_url": {"url": f"data:image/jpeg;base64,{image_to_base64(image)}"}
27
- }
28
- ]
29
- }
30
- ],
31
- "private": "true"
32
- }
33
- response = requests.post(API_URL, json=data)
34
- if response.ok:
35
- return response.json()["choices"][0]["message"]["content"]
36
- return ""
37
 
38
  gr.Interface(
39
  caption,
 
3
  from io import BytesIO
4
 
5
  import gradio as gr
6
+ from huggingface_hub import InferenceClient
7
 
 
8
  PROMPT = os.environ.get("PROMPT", "Describe this image.")
9
 
10
+ client = InferenceClient(model="https://text.pollinations.ai/openai")
11
+
12
  def image_to_base64(image):
13
  buf = BytesIO()
14
  image.save(buf, "JPEG")
15
+ buf.seek(0)
16
+ return f"data:image/jpeg;base64,{base64.b64encode(buf.getvalue()).decode()}"
17
 
18
  def caption(image):
19
+ out = ""
20
+ for chunk in client.chat.completions.create(
21
+ model="openai-large",
22
+ messages=[{
23
+ "role": "user",
24
+ "content": [
25
+ {"type": "text", "text": PROMPT},
26
+ {"type": "image_url", "image_url": {"url": image_to_base64(image)}}
27
+ ]
28
+ }],
29
+ stream=True
30
+ ):
31
+ out += chunk.choices[0].delta.content
32
+ yield out
 
 
 
 
 
 
33
 
34
  gr.Interface(
35
  caption,