import gradio as gr
from huggingface_hub import InferenceClient
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
import groq
import warnings
import asyncio
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.llms.groq import Groq
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
warnings.filterwarnings("ignore", message=".*clean_up_tokenization_spaces.*")
theme = gr.themes.Soft(
primary_hue=gr.themes.Color(c100="#cde3fb", c200="#cde3fb", c300="#81b9f4", c400="#4f9df0", c50="rgba(221.44736842105263, 237.49427917620136, 255, 1)", c500="#0473ea", c600="#0473ea", c700="#0473ea", c800="#0473ea", c900="#0083B3", c950="#0083b3"),
secondary_hue=gr.themes.Color(c100="#d7f6cc", c200="#d7f6cc", c300="#9be880", c400="#9be880", c50="#d7f6cc", c500="#74df4c", c600="#38d200", c700="#38d200", c800="rgba(50.43333333333328, 189.125, 0, 1)", c900="rgba(41.409166666666614, 155.28437499999998, 0, 1)", c950="#134e28"),
neutral_hue=gr.themes.Color(c100="#e5e5e5", c200="#d4d4d4", c300="#a8a9aa", c400="#a8a9aa", c50="#f9fafb", c500="rgb(134, 135, 136)", c600="rgb(134, 135, 136)", c700="#525355", c800="#525355", c900="rgba(52.90131578947368, 53.54254385964912, 54.82499999999999, 1)", c950="#353637"),
font=[gr.themes.GoogleFont('Noto Sans KR'), gr.themes.GoogleFont('Noto Sans KR'), gr.themes.GoogleFont('Noto Sans KR'), gr.themes.GoogleFont('Noto Sans KR')],
font_mono=[gr.themes.GoogleFont('Noto Sans KR'), gr.themes.GoogleFont('Noto Sans KR'), gr.themes.GoogleFont('Noto Sans KR'), gr.themes.GoogleFont('Noto Sans KR')],
)
"""
RAG Custom Functins
"""
# Global variables
index = None
query_engine = None
# Initialize Groq LLM and ensure it is used
llm = Groq(model="mixtral-8x7b-32768")
Settings.llm = llm # Ensure Groq is the LLM being used
# Initialize our chosen embedding model
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
def load_documents(file_objs):
global index, query_engine
try:
if not file_objs:
return "Error: No files selected."
documents = []
document_names = []
for file_obj in file_objs:
document_names.append(file_obj.name)
loaded_docs = SimpleDirectoryReader(input_files=[file_obj.name]).load_data()
documents.extend(loaded_docs)
if not documents:
return "No documents found in the selected files."
# Create index from documents using Groq LLM and HuggingFace Embeddings
index = VectorStoreIndex.from_documents(
documents,
llm=llm, # Ensure Groq is used here
embed_model=embed_model
)
# Create query engine
query_engine = index.as_query_engine()
return f"Successfully loaded {len(documents)} documents from the files: {', '.join(document_names)}"
except Exception as e:
return f"Error loading documents: {str(e)}"
async def perform_rag(query, history):
global query_engine
if query_engine is None:
return history + [("Please load documents first.", None)]
try:
response = await asyncio.to_thread(query_engine.query, query)
return history + [(query, str(response))]
except Exception as e:
return history + [(query, f"Error processing query: {str(e)}")]
def clear_all():
global index, query_engine
index = None
query_engine = None
return None, "", [], "" # Reset file input, load output, chatbot, and message input to default states
"""
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
"""
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
# gr.Slider(
# minimum=0.1,
# maximum=1.0,
# value=0.95,
# step=0.05,
# label="Top-p (nucleus sampling)",
def respond(
message,
history: list[tuple[str, str]],
system_message="당신은 스탠다드차타드은행 한국 지사의 친절하고 유능한 AI 어시스턴트입니다. 한국어로 원활하게 소통하며, 번역, 문서 작성, 그리고 다양한 업무 지원을 신속하고 정확하게 처리합니다. 필요한 정보를 제공하고, 직원들이 업무를 효율적으로 수행할 수 있도록 돕는 역할을 수행합니다.",
max_tokens=512,
temperature=0.2,
top_p=0.95,
):
messages = [{"role": "system", "content": system_message}]
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
response = ""
for message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.content
response += token
yield response
# """
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
# """
# Define the functions for each tab
def scbk_gpt_response(user_message):
# Placeholder function for SCBK-GPT response
response = f"SCBK-GPT: {user_message}"
return response
def data_analysis(data):
# Placeholder function for data analysis
df = pd.DataFrame(data)
summary = df.describe()
return summary
def rag_response(query, document):
# Placeholder function for RAG response
# Here you would implement the logic to process the query and the document
return f"RAG: {query} from {document.name}"
def agentic_ai_response(task):
# Placeholder function for Agentic AI response
return f"Agentic AI: {task}"
custom_css = """
.contain {
display: flex;
flex-direction: column;
height: 100vh;
}
#custom_chatbot {
flex-grow: 1;
display: flex;
flex-direction: column;
}
#custom_chatbot .gr-chatbot {
flex-grow: 1;
overflow-y: auto;
}
"""
# Create the Gradio app with the Soft theme
with gr.Blocks(theme=theme, fill_height=True, css=custom_css) as demo:
# Add a banner with a title and a logo
with gr.Row():
#gr.Image("/file=logo.png", elem_classes="app-logo", show_download_button=False, width=200) # Logo on the left
# gr.Markdown("")
# gr.Markdown("# SCBK-GPT Demo by AI Usage 1조", elem_classes="app-title") # Title on the right
gr.Markdown(
"""