File size: 2,657 Bytes
4966832
 
 
cc431b5
4966832
4f36f3a
93e2858
bcea1f2
4966832
cc431b5
4966832
 
 
 
 
 
cc431b5
4f36f3a
4966832
 
 
 
cc431b5
 
9589466
4966832
4f36f3a
4966832
 
 
 
 
 
cc431b5
4966832
 
 
 
 
cc431b5
4966832
cc431b5
4966832
cc431b5
 
 
4966832
 
 
 
 
 
 
 
 
 
 
 
 
8c07d46
4f36f3a
 
cc431b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcea1f2
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import gradio as gr
import copy
import time
import llama_cpp
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
import os
from gradio.components import Image, Text


llm = Llama(
    model_path=hf_hub_download(
        repo_id=os.environ.get("REPO_ID", "TheBloke/Llama-2-7B-Chat-GGML"),
        filename=os.environ.get("MODEL_FILE", "llama-2-7b-chat.ggmlv3.q5_0.bin"),
    ),
    n_ctx=2048,
    n_gpu_layers=50, # change n_gpu_layers if you have more or less VRAM 
)

history = []

system_message = """
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.
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.
"""


def generate_text(message, history):
    temp = ""
    input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n "
    for interaction in history:
        input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] "

    input_prompt = input_prompt + str(message) + " [/INST] "

    output = llm(
        input_prompt,
        temperature=0.15,
        top_p=0.1,
        top_k=40, 
        repeat_penalty=1.1,
        max_tokens=1024,
        stop=[
            "<|prompter|>",
            "<|endoftext|>",
            "<|endoftext|> \n",
            "ASSISTANT:",
            "USER:",
            "SYSTEM:",
        ],
        stream=True,
    )
    for out in output:
        stream = copy.deepcopy(out)
        temp += stream["choices"][0]["text"]
        yield temp

    history = ["init", input_prompt]


def predict(img):
    img = PILImage.create(img)
    pred,pred_idx,probs = learn.predict(img)
    return {labels[i]: float(probs[i]) for i in range(len(labels))}

title = "Bird Detector"
description = "Bird Detector."
examples = ['BIRD.png']
interpretation='default'
enable_queue=True

def combined(img, message):
    prediction = predict(img)
    response = generate_text(message, history)
    if "I have detected" in response:
        response = response.replace("I have detected", f"I have detected {prediction['bird']} in the image.")

    return response




gr.Interface(fn=combined,inputs=Image(shape=(512, 512)),outputs=Text(),title=title,description=description,examples=examples,interpretation=interpretation,enable_queue=enable_queue).launch()