FengHou97 commited on
Commit
a6d4446
·
verified ·
1 Parent(s): 0dd7d52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -1
app.py CHANGED
@@ -3,7 +3,25 @@ import gradio as gr
3
  from transformers import pipeline
4
  import numpy as np
5
  from PIL import Image
 
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  pipes = {
9
  "ViT/B-16": pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch16"),
@@ -35,7 +53,15 @@ images="festival.jpg"
35
 
36
  def shot(image, labels_text, model_name, hypothesis_template_prefix, hypothesis_template_suffix, domains_text):
37
  labels = [label.strip(" ") for label in labels_text.strip(" ").split(",")]
38
- domains = [domain.strip(" ") for domain in domains_text.strip(" ").split(",")]
 
 
 
 
 
 
 
 
39
  hypothesis_template = hypothesis_template_prefix + ' ' + hypothesis_template_suffix.format(*domains)
40
  print(hypothesis_template)
41
 
 
3
  from transformers import pipeline
4
  import numpy as np
5
  from PIL import Image
6
+ from dotenv import load_dotenv
7
+ import google.generativeai as genai
8
 
9
+ load_dotenv()
10
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API")
11
+ genai.configure(api_key=GOOGLE_API_KEY)
12
+ model_vision = genai.GenerativeModel('gemini-pro-vision')
13
+
14
+ def gemini_response_vision(input_texts, image):
15
+ try:
16
+ if input_texts != "":
17
+ response2 = model_vision.generate_content([input_texts, image])
18
+ else:
19
+ response2 = model_vision.generate_content(image)
20
+
21
+ return response2.text
22
+
23
+ except Exception as e:
24
+ raise e
25
 
26
  pipes = {
27
  "ViT/B-16": pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch16"),
 
53
 
54
  def shot(image, labels_text, model_name, hypothesis_template_prefix, hypothesis_template_suffix, domains_text):
55
  labels = [label.strip(" ") for label in labels_text.strip(" ").split(",")]
56
+
57
+ if not domains_text == '':
58
+ domains = [domain.strip(" ") for domain in domains_text.strip(" ").split(",")]
59
+ else:
60
+ img = Image.open(image)
61
+ input_text = "Please describe the image"
62
+ domains = gemini_response_vision(input_texts=input_text, image=img)
63
+ print(domains)
64
+
65
  hypothesis_template = hypothesis_template_prefix + ' ' + hypothesis_template_suffix.format(*domains)
66
  print(hypothesis_template)
67