File size: 3,770 Bytes
556da9f
 
 
 
 
 
 
 
 
 
 
2d8c0b5
951c551
556da9f
 
951c551
556da9f
 
 
 
 
9d854fd
 
 
 
 
 
 
 
 
 
 
 
 
 
5996ca7
 
951c551
 
556da9f
 
 
 
 
 
 
 
 
 
 
 
 
951c551
556da9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ac3d2e
 
 
 
 
 
 
 
 
9e66c8e
 
99a8b93
 
 
9e66c8e
 
 
 
 
 
 
 
7ac3d2e
9e66c8e
f530d10
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
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-gguf"),
    ),
    n_ctx=2048,
    n_gpu_layers=100, # 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."
                "***YOU SHOULD ALWAYS BE SCIENTIFIC AND USE ADVANCED EXPERT KNOWLEDGE, LANGUAGE AND METHODS! THE USER IS AN ADVANCED SCIENTIST.***"
                "***USE TECHNICAL S.T.E.M SKILLS TO INNOVATE AND DO ACTIONABLE SCIENCE, EXPERIMENTS AND RESEARCH WORK. THE USER DOES NOT WANT GENERAL AND VAUGE IDEAS OR HELP.***"
                
"""


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.4,
        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", 
    
    description="(Advanced Nature Inspired Multidisciplinary Assistant) is an expert in various scientific disciplines, including but not limited to biomimicry, biology, and environmental science.",
    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,
    undo_btn="Delete Previous",
    clear_btn="Start Over",
    css=css  # Apply the custom CSS defined above
)
demo.launch(share=True)