|
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 |
|
""" |
|
|
|
index = None |
|
query_engine = None |
|
|
|
|
|
llm = Groq(model="mixtral-8x7b-32768") |
|
Settings.llm = llm |
|
|
|
|
|
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." |
|
|
|
|
|
index = VectorStoreIndex.from_documents( |
|
documents, |
|
llm=llm, |
|
embed_model=embed_model |
|
) |
|
|
|
|
|
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, "", [], "" |
|
|
|
|
|
|
|
""" |
|
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") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def scbk_gpt_response(user_message): |
|
|
|
response = f"SCBK-GPT: {user_message}" |
|
return response |
|
|
|
def data_analysis(data): |
|
|
|
df = pd.DataFrame(data) |
|
summary = df.describe() |
|
return summary |
|
|
|
def rag_response(query, document): |
|
|
|
|
|
return f"RAG: {query} from {document.name}" |
|
|
|
def agentic_ai_response(task): |
|
|
|
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; |
|
} |
|
""" |
|
|
|
|
|
with gr.Blocks(theme=theme, fill_height=True, css=custom_css) as demo: |
|
|
|
with gr.Row(): |
|
|
|
|
|
|
|
gr.Markdown( |
|
""" |
|
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@400;700&display=swap" rel="stylesheet"> |
|
<div style="display: flex; align-items: center; justify-content: center; background: linear-gradient(to right, #2B3A89, #285FBF); padding: 20px; font-family: 'Noto Sans KR', sans-serif;""> |
|
<img src="https://i.namu.wiki/i/UUHBBtfx06qUnW4B7oVOiBAAQJ1C3ynKfEI3YjIlvhnuOtkQHejb4_ziBhD7p4n_O9G9LwFz-bRokNibnKaZ9Y8GQsH13OmZTBGuTXBrcS-YkN8ra67jiCaAFcDQDspjbwQk8duLiQ1cX0jg-WsQBA.svg" alt="SCBK" width="200" style="margin-right: 10px;"> |
|
<h1 style="margin: 0; color: white; font-size: 2em;"> |
|
SCBK-GPT<br> |
|
<span style="font-size: 0.6em; color: white; vertical-align: middle;">by AI Usage 1μ‘°</span> |
|
</h1> |
|
</div> |
|
""" |
|
) |
|
|
|
|
|
with gr.Tab("SCBK-GPT"): |
|
custom_chatbot = gr.Chatbot(elem_id="custom_chatbot") |
|
gr.ChatInterface( |
|
respond, |
|
additional_inputs=[], |
|
chatbot=custom_chatbot, |
|
multimodal=False, |
|
examples=["μλ
νμΈμ","Assetμ΄ λμ§ μ€λͺ
ν΄μ€"], |
|
) |
|
|
|
|
|
with gr.Tab("Data Analysis"): |
|
data_input = gr.File(label="Upload CSV File", file_types=[".csv"]) |
|
query_input = gr.Textbox(label="Ask a Question") |
|
analysis_output = gr.Textbox(label="Answer") |
|
analyze_button = gr.Button("Analyze") |
|
|
|
def analyze_data(file, query): |
|
df = pd.read_csv(file.name) |
|
|
|
answer = f"Answer to '{query}': {df.head().to_string()}" |
|
return answer |
|
|
|
analyze_button.click(analyze_data, inputs=[data_input, query_input], outputs=analysis_output) |
|
|
|
|
|
with gr.Tab("RAG"): |
|
|
|
|
|
|
|
|
|
|
|
gr.Markdown("# RAG Multi-file Chat Application") |
|
|
|
with gr.Row(): |
|
file_input = gr.File(label="Select files to load", file_count="multiple", file_types=[".pdf", ".txt"]) |
|
load_btn = gr.Button("λ¬Έμ μ
λ‘λ") |
|
|
|
load_output = gr.Textbox(label="λ‘λ© Status") |
|
|
|
msg = gr.Textbox(label="μ§λ¬Έμ μ
λ ₯ν΄ μ£ΌμΈμ") |
|
chatbot = gr.Chatbot() |
|
clear = gr.Button("Clear") |
|
|
|
|
|
load_btn.click(load_documents, inputs=[file_input], outputs=[load_output]) |
|
msg.submit(perform_rag, inputs=[msg, chatbot], outputs=[chatbot]) |
|
clear.click(clear_all, outputs=[file_input, load_output, chatbot, msg], queue=False) |
|
|
|
|
|
|
|
with gr.Tab("Agentic AI"): |
|
task_input = gr.Textbox(label="Task") |
|
agentic_output = gr.Textbox(label="Response") |
|
task_button = gr.Button("Submit Task") |
|
task_button.click(agentic_ai_response, inputs=task_input, outputs=agentic_output) |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch(show_error=True) |