|
import torch |
|
import pandas as pd |
|
import numpy as np |
|
import gradio as gr |
|
import re |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
import re |
|
from huggingface_hub import login |
|
import os |
|
|
|
|
|
TOKEN = os.getenv('HF_AUTH_TOKEN') |
|
login(token=TOKEN, |
|
add_to_git_credential=False) |
|
|
|
|
|
API_KEY = os.getenv('OPEN_AI_API_KEY') |
|
|
|
DESCRIPTION = ''' |
|
<div> |
|
<h1 style="text-align: center;">Amphisbeana π</h1> |
|
<p>This uses Llama 3 and GPT-4o as generation, both of these make the final generation. <a href="https://huggingface.co/meta-llama/Meta-Llama-3-8B"><b>Llama3-8b</b></a>and <a href="https://platform.openai.com/docs/models/gpt-4o"><b>GPT-4o</b></a></p> |
|
</div> |
|
''' |
|
|
|
|
|
llama_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B") |
|
llama_model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B", token=TOKEN, torch_dtype=torch.float16).to('cuda') |
|
|
|
|
|
def llama_generation(input_text: str, |
|
history): |
|
""" |
|
Pass input texts, tokenize, output and back to text. |
|
""" |
|
|
|
|
|
header = '''Your are a helpful AI called amphisbeana. |
|
You will help the user, by giving accurate but creative response''' |
|
|
|
input_ids = llama_tokenizer.encode(input_text + header, |
|
return_tensors='pt').to('cuda') |
|
|
|
|
|
output_ids = llama_model.generate(input_ids=input_ids) |
|
|
|
|
|
output_text = llama_tokenizer.decode(output_ids[0], |
|
skip_special_tokens=True) |
|
|
|
return output_text |
|
|
|
|
|
|
|
|
|
chatbot=gr.Chatbot(height=600, label="Amphisbeana AI") |
|
|
|
with gr.Blocks(fill_height=True) as demo: |
|
gr.Markdown(DESCRIPTION) |
|
gr.ChatInterface( |
|
fn=llama_generation, |
|
chatbot=chatbot, |
|
fill_height=True, |
|
examples=["Make a poem of batman inside willy wonka", |
|
"How can you a burrito with just flour?", |
|
"How was saturn formed in 3 sentences", |
|
"How does the frontal lobe effect playing soccer"], |
|
cache_examples=False |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |