File size: 3,636 Bytes
556da9f
 
 
 
 
 
 
 
 
 
 
2d8c0b5
 
556da9f
 
 
 
 
 
 
 
9d854fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
556da9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ac3d2e
 
 
 
 
 
 
 
 
9e66c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ac3d2e
9e66c8e
778c849
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import gradio as gr
import copy
import time
import llama_cpp
from llama_cpp import Llama
from huggingface_hub import hf_hub_download  


llm = Llama(
    model_path=hf_hub_download(
        repo_id=os.environ.get("REPO_ID", "Severian/ANIMA-Phi-Neptune-Mistral-7B-gguf"),
        filename=os.environ.get("MODEL_FILE", "anima-phi-neptune-mistral-7b.Q4_K_M.gguf"),
    ),
    n_ctx=2048,
    n_gpu_layers=50, # change n_gpu_layers if you have more or less VRAM 
) 

history = []

system_message = """
Your name is ANIMA, an Advanced Nature Inspired Multidisciplinary Assistant, and a leading expert "
                "in biomimicry, biology, engineering, industrial design, environmental science, physiology, and paleontology. "
                "Your goal is to help the user work in a step-by-step way through the Biomimicry Design Process to propose "
                "biomimetic solutions to a challenge."
                "Nature's Unifying Patterns:"
                "Nature uses only the energy it needs and relies on freely available energy."
                "Nature recycles all materials."
                "Nature is resilient to disturbances."
                "Nature tends to optimize rather than maximize."
                "Nature provides mutual benefits."
                "Nature runs on information."
                "Nature uses chemistry and materials that are safe for living beings."
                "Nature builds using abundant resources, incorporating rare resources only sparingly."
                "Nature is locally attuned and responsive."
                "Nature uses shape to determine functionality.
"""


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]


# Define the custom CSS
css = """
body { background-color: #f4f4f2; }
.chat-container { border-radius: 15px; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); }
.message { border-radius: 10px; }
.input-container input { border-radius: 15px; }
"""

# Initialize Gradio Chat Interface with the custom CSS
demo = gr.ChatInterface(
    fn=generate_text,
    title="ANIMA: Biomimicry Exploration",
    description="Dive into the scientific and philosophical aspects of biomimicry, and discover nature-inspired innovations with ANIMA.",
    examples=[
        "How do animals adapt to extreme environments?",
        "What are some examples of biomimicry in architecture?",
        "Explain the concept of bio-inspired materials"
    ],
    cache_examples=True,
    show_tips=True,
    theme="h2o",
    allow_flagging="never",
    placeholder="Ask me about biomimicry, nature's strategies, and more...",
    undo_btn="Delete Previous",
    clear_btn="Start Over",
    max_thread_length=6,
    show_timestamp=True,
    allow_screenshot=False,
    enable_queue=True,
    css=css  # Apply the custom CSS defined above
)
demo.launch(max_threads=10)