lyimo commited on
Commit
b3437e3
Β·
1 Parent(s): 45e13cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -24
app.py CHANGED
@@ -1,29 +1,33 @@
1
  import gradio as gr
2
  import copy
 
3
  import time
4
  import llama_cpp
5
  from llama_cpp import Llama
6
  from huggingface_hub import hf_hub_download
7
- import os
8
- from gradio.components import Image, Text
9
-
10
 
 
11
  llm = Llama(
12
  model_path=hf_hub_download(
13
  repo_id=os.environ.get("REPO_ID", "TheBloke/Llama-2-7B-Chat-GGML"),
14
  filename=os.environ.get("MODEL_FILE", "llama-2-7b-chat.ggmlv3.q5_0.bin"),
15
  ),
16
  n_ctx=2048,
17
- n_gpu_layers=50, # change n_gpu_layers if you have more or less VRAM
18
  )
19
 
20
  history = []
21
 
22
  system_message = """
23
- You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
24
- If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
25
  """
 
 
 
 
26
 
 
27
 
28
  def generate_text(message, history):
29
  temp = ""
@@ -37,13 +41,13 @@ def generate_text(message, history):
37
  input_prompt,
38
  temperature=0.15,
39
  top_p=0.1,
40
- top_k=40,
41
  repeat_penalty=1.1,
42
  max_tokens=1024,
43
  stop=[
44
- "<|prompter|>",
45
- "<|endoftext|>",
46
- "<|endoftext|> \n",
47
  "ASSISTANT:",
48
  "USER:",
49
  "SYSTEM:",
@@ -57,28 +61,35 @@ def generate_text(message, history):
57
 
58
  history = ["init", input_prompt]
59
 
60
-
61
  def predict(img):
62
  try:
63
  img = PILImage.create(img)
64
  except:
65
  return {"bird": "Unknown"}
66
- pred,pred_idx,probs = learn.predict(img)
67
- return {labels[i]: float(probs[i]) for i in range(len(labels))}
68
 
69
- title = "Bird Detector"
70
- description = "Bird Detector."
71
- examples = ['BIRD.png']
72
- interpretation='default'
73
- enable_queue=True
74
 
75
  def combined(img, message):
76
  prediction = predict(img)
77
- response = generate_text(message, history)
78
- if "I have detected" in response:
79
- response = response.replace("I have detected", f"I have detected {prediction['bird']} in the image.")
80
-
81
  return response
82
 
83
-
84
- gr.Interface(fn=combined,inputs=Image(shape=(512, 512)),outputs=Text(),title=title,description=description,examples=examples,interpretation=interpretation,enable_queue=enable_queue).launch()
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import copy
3
+ import os
4
  import time
5
  import llama_cpp
6
  from llama_cpp import Llama
7
  from huggingface_hub import hf_hub_download
8
+ from fastai.vision.all import *
 
 
9
 
10
+ # Load the LLM model
11
  llm = Llama(
12
  model_path=hf_hub_download(
13
  repo_id=os.environ.get("REPO_ID", "TheBloke/Llama-2-7B-Chat-GGML"),
14
  filename=os.environ.get("MODEL_FILE", "llama-2-7b-chat.ggmlv3.q5_0.bin"),
15
  ),
16
  n_ctx=2048,
17
+ n_gpu_layers=50, # change n_gpu_layers if you have more or less VRAM
18
  )
19
 
20
  history = []
21
 
22
  system_message = """
23
+ You are a BIRD EXPERT
 
24
  """
25
+ # The rest of the system message
26
+
27
+ # Load the Vision Model
28
+ learn = load_learner('export.pkl')
29
 
30
+ labels = learn.dls.vocab
31
 
32
  def generate_text(message, history):
33
  temp = ""
 
41
  input_prompt,
42
  temperature=0.15,
43
  top_p=0.1,
44
+ top_k=40,
45
  repeat_penalty=1.1,
46
  max_tokens=1024,
47
  stop=[
48
+ "",
49
+ "",
50
+ " \n",
51
  "ASSISTANT:",
52
  "USER:",
53
  "SYSTEM:",
 
61
 
62
  history = ["init", input_prompt]
63
 
 
64
  def predict(img):
65
  try:
66
  img = PILImage.create(img)
67
  except:
68
  return {"bird": "Unknown"}
69
+ pred, pred_idx, probs = learn.predict(img)
70
+ return {"bird": labels[pred_idx], "probs": {labels[i]: float(probs[i]) for i in range(len(labels))}}
71
 
72
+ title = "Bird Detector with LLM"
73
+ description = "Detect birds and get LLM responses."
74
+ examples = [{"img": "BIRD.jpg", "message": "Tell me about the bird."}]
75
+ interpretation = 'default'
76
+ enable_queue = True
77
 
78
  def combined(img, message):
79
  prediction = predict(img)
80
+ response = list(generate_text(f"I have detected {prediction['bird']} in the image. {message}", history))
 
 
 
81
  return response
82
 
83
+ gr.Interface(
84
+ fn=combined,
85
+ inputs=[
86
+ gr.inputs.Image(),
87
+ gr.inputs.Textbox(label="Message to LLM")
88
+ ],
89
+ outputs=gr.outputs.Textbox(),
90
+ title=title,
91
+ description=description,
92
+ examples=examples,
93
+ interpretation=interpretation,
94
+ enable_queue=enable_queue,
95
+ ).launch()