SFP commited on
Commit
5fd680d
·
1 Parent(s): a7a9bdc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -2
app.py CHANGED
@@ -1,9 +1,31 @@
1
  import gradio as gr
2
  from PIL import Image
 
 
 
 
 
 
 
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def image_predict(image):
5
- # ... code to predict the class of the image goes here...
6
- pass
7
 
8
  iface = gr.Interface(fn=image_predict, inputs="image", outputs="label")
9
  iface.launch()
 
1
  import gradio as gr
2
  from PIL import Image
3
+ import requests
4
+ import base64
5
+ import openai
6
+ openai.api_key = ""
7
+ openai.api_base = "https://api.deepinfra.com/v1/openai"
8
+ def todataimage(file, ext):
9
+ binary_fc = file
10
+ base64_utf8_str = base64.b64encode(binary_fc).decode('utf-8')
11
 
12
+ dataurl = f'data:image/{ext};base64,{base64_utf8_str}'
13
+ return dataurl
14
+ def caption(file, ext):
15
+ response = requests.post("https://russellc-comparing-captioning-models.hf.space/run/predict", json={
16
+ "data": [
17
+ todataimage(file, ext),
18
+ ]}).json()
19
+
20
+ data = response["data"]
21
+ chat_completion = openai.ChatCompletion.create(
22
+ model="meta-llama/Llama-2-70b-chat-hf",
23
+ messages=[{"role": "system", "content": "you will be given descriptions of one image from a varity of image captioning models with a varity of quality, what you need to do is combine them into one image caption and make that be your output, no extras words like \"here is your output\", remeber, don't take too much information from low quality, or too little from high. do NOT contain ANY text other than the description"},{"role":"user", "content":"High Quality:\n"+data[1]+"\n"+data[3]+"\nMedium Qualitt:\n"+data[2]+"\nLow Quality\n"+data[0]}],
24
+ )
25
+
26
+ return chat_completion.choices[0].message.content
27
  def image_predict(image):
28
+ return caption(image, "png")
 
29
 
30
  iface = gr.Interface(fn=image_predict, inputs="image", outputs="label")
31
  iface.launch()