alperenunlu commited on
Commit
f08f9cc
1 Parent(s): 0a064dd

Init commit

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +56 -0
  3. pets.pkl +3 -0
  4. requirements.txt +6 -0
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: My Vet
3
- emoji: 🏃
4
  colorFrom: purple
5
  colorTo: pink
6
  sdk: gradio
 
1
  ---
2
  title: My Vet
3
+ emoji: 🐈
4
  colorFrom: purple
5
  colorTo: pink
6
  sdk: gradio
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import google.generativeai as genai
4
+ import gradio as gr
5
+ from fastai.learner import load_learner
6
+ from PIL import Image
7
+
8
+ API_KEY = os.getenv("GENAI_API_KEY")
9
+
10
+ genai.configure(api_key=API_KEY)
11
+
12
+ model = genai.GenerativeModel(model_name="gemini-pro-vision")
13
+
14
+ learn = load_learner("pets.pkl")
15
+ categories = learn.vocab
16
+
17
+ prompt = (
18
+ "You are and animal expert and a veterinarian.\n"
19
+ "Give your expert opinion on the following questions based on"
20
+ "the image of the animal.\n"
21
+ "Only answer answer the last question.\n"
22
+ "Don't give anything besides the answer."
23
+ )
24
+
25
+
26
+ def classify_image(img):
27
+ pred, idx, probs = learn.predict(img)
28
+ return dict(zip(categories, map(float, probs)))
29
+
30
+
31
+ def random_response(message, history, image=None):
32
+ if image is not None:
33
+ image = Image.fromarray(image)
34
+ message = "Q: " + message.strip()
35
+ history = history[-5:] if len(history) > 0 else ""
36
+ history = "\n".join([f"Q: {i[0]}\nA:{i[1]}\n" for i in history])
37
+ message = prompt + '\n\n' + history + '\n' + message + "\nA: "
38
+ print(f"The new message is : \n{message}")
39
+ return model.generate_content([message, image]).text
40
+ else:
41
+ return "Please provide an image."
42
+
43
+
44
+ with gr.Blocks() as demo:
45
+ image = gr.Image()
46
+ label = gr.Label(num_top_classes=5)
47
+ gr.Interface(
48
+ classify_image,
49
+ inputs=image,
50
+ outputs=label,
51
+ title="Pet Classifier",
52
+ description="Classify an image of a pet into different categories.",
53
+ )
54
+ gr.ChatInterface(random_response, additional_inputs=[image])
55
+
56
+ demo.launch()
pets.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8011b96bc737fa4b6876a4104a11ae89550a5e908e78162271b1c664165d38dd
3
+ size 102826302
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastai
2
+ gradio
3
+ timm
4
+ google-generativeai
5
+ torch
6
+ torchvision