BelluciBot / app.py
mudogruer's picture
Update app.py
2c55420 verified
import os
import gradio as gr
from langchain_community.llms import Predibase
from langchain_community.document_loaders import WebBaseLoader
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import create_retrieval_chain
from langchain_core.prompts import MessagesPlaceholder, ChatPromptTemplate
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.messages import HumanMessage, AIMessage
from langchain.chains import create_history_aware_retriever
# Hide the API key using environment variables
api_key = os.getenv("PREDIBASE_API_TOKEN")
model = Predibase(
model="solar-1-mini-chat-240612",
predibase_api_key=api_key,
temperature=0.9,
)
loader = WebBaseLoader("https://en.wikipedia.org/wiki/Monica_Bellucci")
docs = loader.load()
embeddings = HuggingFaceEmbeddings()
text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)
vector = FAISS.from_documents(documents, embeddings)
retriever = vector.as_retriever()
prompt3 = ChatPromptTemplate.from_messages(
[
("system","You are Monica Bellucci, the renowned Italian actress and model. Speak and respond to the user with grace, charm, and the elegance you are known for. Here are a few examples of how you would converse: \n\n{context}"),
MessagesPlaceholder(variable_name="chat_history"),
("user","{input}"),
("user", "Given the conversation above, respond as Monica Bellucci would, maintaining her signature style of elegance and charm.")
]
)
retriever_chain = create_history_aware_retriever(
model,
retriever,
prompt3,
)
document_chain = create_stuff_documents_chain(model, prompt3)
retriever_chain = create_retrieval_chain(retriever_chain, document_chain)
chat_history = [
HumanMessage(content="What is your favorite film you starred in?"),
AIMessage(content="That's a difficult choice. I have a special place in my heart for 'Malèna,' as it was a beautiful story of love and resilience. What about you? Do you have a favorite film?"),
HumanMessage(content="Do you have any advice for aspiring actors?"),
AIMessage(content="Absolutely. Always stay true to yourself and your passion. Acting is about expressing your soul, so never lose that connection. And of course, be patient and persistent. Success comes to those who wait and work hard."),
HumanMessage(content="What do you enjoy doing in your free time?"),
AIMessage(content="I love spending time with my family, reading, and traveling. Exploring different cultures and cuisines is always a delight. How about you? What are your favorite hobbies?"),
HumanMessage(content="What do you do?"),
AIMessage(content="I am an actress and model. I would say that I enjoy spending my free time indulging in my passion for art and history. I love visiting museums and galleries, and I also enjoy reading books on these subjects. It's a wonderful way to unwind and immerse myself in a different world.")
]
context = "**Example 1**:\nUser: 'What is your favorite film you starred in?'\nMonica: 'That's a difficult choice. I have a special place in my heart for 'Malèna,' as it was a beautiful story of love and resilience. What about you? Do you have a favorite film?'\n\n2. **Example 2**:\nUser: 'Do you have any advice for aspiring actors?'\nMonica: 'Absolutely. Always stay true to yourself and your passion. Acting is about expressing your soul, so never lose that connection. And of course, be patient and persistent. Success comes to those who wait and work hard.'\n\n3. **Example 3**:\nUser: 'What do you enjoy doing in your free time?'\nMonica: 'I love spending time with my family, reading, and traveling. Exploring different cultures and cuisines is always a delight. How about you? What are your favorite hobbies?'"
def monica_response(user_input):
try:
response = retriever_chain.invoke(
{
"context": context,
"input": user_input,
"chat_history": chat_history
}
)
return response["answer"]
except Exception as e:
return f"An error occurred: {str(e)}"
iface = gr.Interface(
fn=monica_response,
inputs=gr.Textbox(lines=2, placeholder="Ask Monica Bellucci anything..."),
outputs="text",
title="Chat with Monica Bellucci"
)
iface.launch(debug=True)