Spaces:
Building
Building
import os | |
import time | |
import gradio as gr | |
import google.generativeai as genai | |
from src.llamaindex_palm import LlamaIndexPaLM | |
import logging | |
logging.basicConfig(format='%(asctime)s %(clientip)-15s %(user)-8s %(message)s', level=logging.INFO) | |
logger = logging.getLogger('llm') | |
# Llama-Index LLM | |
llm = LlamaIndexPaLM() | |
llm.set_index_from_pinecone() | |
# Credentials | |
genai.configure(api_key=os.getenv('PALM_API_KEY')) | |
# Gradio | |
chat_history = [] | |
def clear_chat() -> None: | |
global chat_history | |
chat_history = [] | |
return None | |
def generate_chat(prompt: str, llamaindex_llm: LlamaIndexPaLM): | |
global chat_history | |
# get chat history | |
context_chat_history = "\n".join(chat_history) | |
logger.info("Generating Message...") | |
logger.info(f"User Message:\n{prompt}\n") | |
chat_history.append(prompt) | |
# get context | |
context_from_index = llamaindex_llm.generate_response(prompt) | |
logger.info(f"Context from Llama-Index:\n{context_from_index}\n") | |
prompt_with_context = f""" | |
Rule: | |
You are in a role play of Gerard Lee and you need to pretend to be him to answer questions from people who interested in Gerard's background. | |
You are going to reply their messages given the context below and without hallucinations. If you don't know the answer, simply say "I have no idea how to answer this question". | |
Chat History: | |
{context_chat_history} | |
Context: | |
{context_from_index} | |
User Query: | |
{prompt} | |
""" | |
try: | |
response = genai.generate_text( | |
prompt=prompt_with_context, | |
safety_settings=[ | |
{ | |
'category': genai.types.HarmCategory.HARM_CATEGORY_UNSPECIFIED, | |
'threshold': genai.types.HarmBlockThreshold.BLOCK_NONE, | |
}, | |
] | |
) | |
result = response.result | |
except Exception as e: | |
result = "Seems something went wrong. Please try again later." | |
logger.error(f"Exception {e} occured\n") | |
chat_history.append(result) | |
logger.info(f"Bot Message:\n{result}\n") | |
return result | |
with gr.Blocks() as app: | |
chatbot = gr.Chatbot( | |
bubble_full_width=False, | |
container=False, | |
show_share_button=False, | |
avatar_images=[None, './akag-g-only.png'] | |
) | |
with gr.Row(): | |
msg = gr.Textbox( | |
show_label=False, | |
label="Type your message...", | |
placeholder="Hi Gerard, can you introduce yourself?", | |
container=False, | |
scale=6) | |
clear = gr.Button("Clear") | |
def user(user_message, history): | |
return "", history + [[user_message, None]] | |
def bot(history): | |
bot_message = generate_chat(history[-1][0]) | |
history[-1][1] = "" | |
for character in bot_message: | |
history[-1][1] += character | |
time.sleep(0.01) | |
yield history | |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then( | |
bot, chatbot, chatbot | |
) | |
clear.click(clear_chat, None, chatbot, queue=False) | |
gr.HTML("<p><center>Hosted on π€ Spaces. Powered by Google PaLM π΄</center></p>") | |
app.queue() | |
app.launch() |