tommy24 commited on
Commit
bce6aeb
·
1 Parent(s): c3a8075

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -29
app.py CHANGED
@@ -719,8 +719,8 @@ state = os.environ.get("state")
719
  system = os.environ.get("system")
720
  auth = os.environ.get("auth")
721
  auth2 = os.environ.get("auth2")
722
- openai.api_key = os.environ.get("auth")
723
- openai.api_base = os.environ.get("host")
724
  vis_url = os.environ.get("vis_url")
725
  vis_auth = os.environ.get("vis_auth")
726
  endpoint = os.environ.get("endpoint")
@@ -783,7 +783,7 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
783
  return None
784
 
785
 
786
- def vision():
787
  # with open("image.png", "wb") as file1_write:
788
  # file1_write.write(image_data)
789
 
@@ -798,38 +798,66 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
798
  if image_data:
799
  try:
800
  # Open the image directly from the image data
801
- image = Image.open(io.BytesIO(image_data))
802
- base64_image_str = encode_image(image)
803
 
804
- payload = {
805
- "content": [
806
- {
807
- "prompt": "What's this image about? or What does this image contains?",
808
- "image": base64_image_str,
809
- }
810
- ],
811
- "token": vis_auth,
812
- }
813
 
814
- url = vis_url
815
- headers = {"Content-Type": "application/json"}
816
 
817
- response = requests.post(url, headers=headers, data=json.dumps(payload))
818
- results = response.json()
819
- results = results["result"]
820
 
821
- answer_index = results.find("Answer:")
822
 
823
- if answer_index != -1:
824
- try:
825
- result_text = results[answer_index + len("Answer:"):].strip()
826
- print(result_text)
827
- return result_text
828
- except:
829
- pass
830
- else:
831
- return "Answer: not found in the string."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
832
 
 
 
 
 
833
  except:
834
  return "ERRRRRRR"
835
  else:
@@ -838,6 +866,7 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
838
 
839
  if UserInput is not None:
840
  caption = UserInput.lower()
 
841
  else:
842
  caption = None
843
 
 
719
  system = os.environ.get("system")
720
  auth = os.environ.get("auth")
721
  auth2 = os.environ.get("auth2")
722
+ openai.api_key = auth
723
+ openai.api_base = host
724
  vis_url = os.environ.get("vis_url")
725
  vis_auth = os.environ.get("vis_auth")
726
  endpoint = os.environ.get("endpoint")
 
783
  return None
784
 
785
 
786
+ def vision(caption):
787
  # with open("image.png", "wb") as file1_write:
788
  # file1_write.write(image_data)
789
 
 
798
  if image_data:
799
  try:
800
  # Open the image directly from the image data
801
+ # image = Image.open(io.BytesIO(image_data))
802
+ # base64_image_str = encode_image(image)
803
 
804
+ # payload = {
805
+ # "content": [
806
+ # {
807
+ # "prompt": "What's this image about? or What does this image contains?",
808
+ # "image": base64_image_str,
809
+ # }
810
+ # ],
811
+ # "token": vis_auth,
812
+ # }
813
 
814
+ # url = vis_url
815
+ # headers = {"Content-Type": "application/json"}
816
 
817
+ # response = requests.post(url, headers=headers, data=json.dumps(payload))
818
+ # results = response.json()
819
+ # results = results["result"]
820
 
821
+ # answer_index = results.find("Answer:")
822
 
823
+ # if answer_index != -1:
824
+ # try:
825
+ # result_text = results[answer_index + len("Answer:"):].strip()
826
+ # print(result_text)
827
+ # return result_text
828
+ # except:
829
+ # pass
830
+ # else:
831
+ # return "Answer: not found in the string."
832
+ payload = json.dumps(
833
+ {
834
+ "messages": [
835
+ "role": "user",
836
+ "content": [
837
+ {
838
+ "type": "text",
839
+ "text": caption
840
+ },
841
+
842
+ {
843
+ "type": "image_url",
844
+ "image_url": Images
845
+ }
846
+ ]
847
+ ],
848
+ "model": "gemini-pro-vision"
849
+ }
850
+ )
851
+ headers = {
852
+ 'Authorization': f'Bearer {auth}',
853
+ 'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
854
+ 'Content-Type': 'application/json'
855
+ }
856
 
857
+ response = requests.request("POST", host, headers=headers, data=payload)
858
+ response = response.json()
859
+ res = response["choices"][0]["message"]["content"]
860
+ return res
861
  except:
862
  return "ERRRRRRR"
863
  else:
 
866
 
867
  if UserInput is not None:
868
  caption = UserInput.lower()
869
+ vision(caption)
870
  else:
871
  caption = None
872