File size: 10,216 Bytes
0d90c0f
 
457096c
 
 
bf82520
 
 
 
 
 
 
 
0d90c0f
457096c
 
 
 
 
 
 
 
 
 
 
 
 
bf82520
 
 
0d90c0f
bf82520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457096c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf82520
457096c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf82520
457096c
 
 
 
bf82520
 
457096c
 
 
 
 
 
bf82520
457096c
 
0d90c0f
457096c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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("<img src='/file=/logo.png' alt='SCBK' width='200'>")
        # gr.Markdown("# SCBK-GPT Demo by AI Usage 1μ‘°", elem_classes="app-title")  # Title on the right
        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>
            """
        )

    # Tab 1: SCBK-GPT
    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이 λ­”μ§€ μ„€λͺ…ν•΄μ€˜"],
        )

    # Tab 2: Data Analysis
    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)
            # Placeholder for actual data analysis logic
            answer = f"Answer to '{query}': {df.head().to_string()}"
            return answer
        
        analyze_button.click(analyze_data, inputs=[data_input, query_input], outputs=analysis_output)

    # Tab 3: RAG
    with gr.Tab("RAG"):
        # document_input = gr.File(label="Upload Document", file_types=[".pdf", ".txt"])
        # query_input = gr.Textbox(label="Query")
        # rag_output = gr.Textbox(label="Response")
        # query_button = gr.Button("Query")
        # query_button.click(rag_response, inputs=[query_input, document_input], outputs=rag_output)
        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")

        # Set up event handlers
        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)


    # Tab 4: Agentic AI
    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)

       
# Launch the app
if __name__ == "__main__":
    demo.launch(show_error=True)