Delete app.py
Browse files
app.py
DELETED
@@ -1,95 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
import os
|
4 |
-
|
5 |
-
# Mock vector database creation
|
6 |
-
vector_db_created = False
|
7 |
-
|
8 |
-
def create_vector_db(uploaded_files):
|
9 |
-
global vector_db_created
|
10 |
-
if uploaded_files:
|
11 |
-
vector_db_created = True
|
12 |
-
return "Vector database created successfully. You can now chat with your documents!"
|
13 |
-
return "Please upload a file first."
|
14 |
-
|
15 |
-
# Initialize Chat Model
|
16 |
-
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
17 |
-
|
18 |
-
def respond(
|
19 |
-
message,
|
20 |
-
history: list[tuple[str, str]],
|
21 |
-
system_message,
|
22 |
-
max_tokens,
|
23 |
-
temperature,
|
24 |
-
top_p,
|
25 |
-
):
|
26 |
-
if not vector_db_created:
|
27 |
-
yield "Error: Please create the vector database first."
|
28 |
-
return
|
29 |
-
|
30 |
-
messages = [{"role": "system", "content": system_message}]
|
31 |
-
for val in history:
|
32 |
-
if val[0]:
|
33 |
-
messages.append({"role": "user", "content": val[0]})
|
34 |
-
if val[1]:
|
35 |
-
messages.append({"role": "assistant", "content": val[1]})
|
36 |
-
messages.append({"role": "user", "content": message})
|
37 |
-
response = ""
|
38 |
-
for message in client.chat_completion(
|
39 |
-
messages,
|
40 |
-
max_tokens=max_tokens,
|
41 |
-
stream=True,
|
42 |
-
temperature=temperature,
|
43 |
-
top_p=top_p,
|
44 |
-
):
|
45 |
-
token = message.choices[0].delta.content
|
46 |
-
response += token
|
47 |
-
yield response
|
48 |
-
|
49 |
-
# Custom CSS
|
50 |
-
css = """
|
51 |
-
#drop-area { border: 2px dashed #42B3CE; border-radius: 10px; padding: 20px; }
|
52 |
-
.error-message { color: red; font-weight: bold; }
|
53 |
-
.vector-btn { background-color: #42B3CE !important; color: white; }
|
54 |
-
.chat-submit { background-color: #06688E !important; color: white; }
|
55 |
-
.chat-clear { background-color: #e0e0e0 !important; color: black; }
|
56 |
-
"""
|
57 |
-
|
58 |
-
def main():
|
59 |
-
with gr.Blocks(css=css) as demo:
|
60 |
-
gr.Markdown("""# **RAG PDF Chatbot**
|
61 |
-
Query your PDF documents! Upload, initialize, and chat using an AI assistant.
|
62 |
-
""")
|
63 |
-
|
64 |
-
# Step 1: File upload and database initialization
|
65 |
-
with gr.Row():
|
66 |
-
with gr.Column():
|
67 |
-
pdf_upload = gr.File(label="Upload PDF documents", file_types=[".pdf"], type="file")
|
68 |
-
create_db_btn = gr.Button("Create vector database", elem_classes=["vector-btn"])
|
69 |
-
db_status = gr.Textbox("Not initialized", interactive=False)
|
70 |
-
|
71 |
-
with gr.Column():
|
72 |
-
gr.Markdown("**Step 2 - Chat with your Document**")
|
73 |
-
chatbot = gr.ChatInterface(
|
74 |
-
respond,
|
75 |
-
additional_inputs=[
|
76 |
-
gr.Textbox(
|
77 |
-
value="You are a helpful assistant...",
|
78 |
-
label="System Message",
|
79 |
-
visible=False
|
80 |
-
),
|
81 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens", visible=False),
|
82 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature", visible=False),
|
83 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p", visible=False),
|
84 |
-
],
|
85 |
-
submit_btn="Submit",
|
86 |
-
clear_btn="Clear",
|
87 |
-
)
|
88 |
-
|
89 |
-
# Button events
|
90 |
-
create_db_btn.click(create_vector_db, inputs=[pdf_upload], outputs=[db_status])
|
91 |
-
|
92 |
-
demo.launch(share=True)
|
93 |
-
|
94 |
-
if __name__ == "__main__":
|
95 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|