GET URL of uploaded image and pass to chat gpt

no because i dont understand the code… Can someone help me implement it in my project?

@stefano.menci for your Mime recommendation, how do i use your code?

Do I replace my own function with yours?

@anvil.server.callable
def saveFile(roomImg):
  row = app_tables.maindata.add_row(usersEmail=anvil.users.get_user()['email'],Office=roomImg)
  rowId = row.get_id()
  return rowId

Where does your function come into play?

@campopianoa Any chance an anvil support specialist can help me figure this out?

This problem persists. Does Anvil have any suggestions on how to fix it? Again ChatGPT code works fine with url off the web, but not with a url from anvil data table. I allowed permissions to the data table and yet it still wont work.

You might be able to build a really straightforward solution with this:

2 Likes

Not very familiar with routing @p.colbert. Can you elaborate on how I would do?

@p.colbert I think I created the server.route correct, but how do I pass it to ChatGPT function?

Server Side Code

@anvil.server.callable
def saveFile(roomImg):
  row = app_tables.maindata.add_row(usersEmail=anvil.users.get_user()['email'],Office=roomImg)
  rowId = row.get_id()
  return rowId

@anvil.server.route('/img_url')
def urlChatGPT(rowId):
  row = app_tables.maindata.get_by_id(rowId)
  img = row['Office']
  return img


@anvil.server.callable
def definingPicture(imgID):
  imgRow = app_tables.maindata.get_by_id(imgID)
  Img = imgRow['Office']
  print(Img.get_url(False))
  NewImg = Img.get_url(False)

  response = client.chat.completions.create( 
    model="gpt-4-vision-preview",
      messages=[
        {
          "role": "user",
          "content": [
            {"type": "text", "text": "List all the furniture in the image. Make sure to describe each furniture as small, medium, or large"},
            {
              'type': 'image_url',
              'image_url': {'url': NewImg },
            },
          ],
        }
      ],
      max_tokens=1500,
    )
  return response.choices[0].message.content

Good for you!

See the explanation in The route decorator. It shows how the URL is put together. Follow that example – with your own App’s “origin”, of course.

I am stuck hehe… I just dont know enough about it to get it to work. :upside_down_face: Agh Thanks for trying to help @p.colbert.

How would i get the url of this route? so i can retrieve the image url?

@anvil.server.route('/img_url/:imgID')
def urlChatGPT(imgID):
    row = app_tables.maindata.get_by_id(imgID)
    img = row['Office']
    print(img.get_url())
    return 



You actually need to return the image’s media object in that function for it to work. You can get the image’s URL from the function by tying it in with app_origin.

See both in this clone:

Thank you so much for making it make sense for me @duncan_richards12 . I understand now what @p.colbert meant.

So I modified the code to fit my project. How Do I use that function in my code? I need to pass the image URL to ChatGPT with my function definingPicture(imgID)?

All server functions use anvil.server.call() in the client side. How do I use this route?

@anvil.server.route("/images/:image_id")
def get_image(image_id):
  # image url
  print(f"{anvil.server.get_app_origin()}/images/{image_id}")
  return app_tables.maindata.get_by_id(image_id)["Office"]



@anvil.server.callable
def definingPicture(imgID):
  imgRow = app_tables.maindata.get_by_id(imgID)
  Img = imgRow['Office']
  print(Img.get_url(False))
  NewImg = Img.get_url(False)

  response = client.chat.completions.create( 
    model="gpt-4-vision-preview",
      messages=[
        {
          "role": "user",
          "content": [
            {"type": "text", "text": "List all the furniture in the image. Make sure to describe each furniture as small, medium, or large"},
            {
              'type': 'image_url',
              'image_url': {'url': NewImg },
            },
          ],
        }
      ],
      max_tokens=1500,
    )
  return response.choices[0].message.content

searched through documentation and forums i keep coming across this

@anvil.server.route("/images/:image_id")
def get_image(image_id):
  # image url
  print(f"{anvil.server.get_app_origin()}/images/{image_id}")
  return app_tables.maindata.get_by_id(image_id)["Office"]

@anvil.server.callable
def hit_endpoint(image_id):
  url = anvil.http.request('anvil.server.get_app_origin()}/images/{image_id}')
  return url

however, anvil.http.request does not exist. How would someone request?

I think the piece you might be missing is that you have the url as defined by the route you’ve created.

your route should probably look like this


@anvil.server.route("/image/:id")
def get_image(id):
    id = anvil.http.url_decode(id)
    return app_tables.my_images.get_by_id(id)['image']

And in your function for open ai

@anvil.server.callable
def defining_picture(img_id):
    img_url = f"{anvil.server.get_app_origin()}/image/{anvil.http.url_encode(img_id)}"
    response = client.chat.completions.create(
        ...
        'image_url': {'url': img_url},
    )

You can test the url by going to an incognito browser window and typing the url directly to see if you get an image!

e.g.

https://favorable-shiny-snail.anvil.app/image/[745589,1102724913]

1 Like

@stucork You are absolutely awesome! You don’t even know how much I appreciate you. It worked officially.

Just for my new self, can you please explain why my method failed and how your http.url_decode/encode worked?

For starters, look at the difference between

and

Those missing details weren’t just for decoration. They’re critical to how each string is interpreted.

Sorry to say, it does exist. See: Making HTTP Requests

Whenever an Anvil feature appears to be “missing”, I strongly recommend that you take advantage of the Search feature at the top of every Documentation page.
image
No Search feature, or Documentation, is ever perfect, or complete, but Anvil’s is better than many I’ve used.

1 Like