Spaces:
Sleeping
Sleeping
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) |