Spaces:
Sleeping
Sleeping
βvinit5112β
commited on
Commit
Β·
42edb57
1
Parent(s):
87cac8f
remove unused
Browse files- backend/app_v2.py +0 -130
backend/app_v2.py
DELETED
@@ -1,130 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from rag import RAG # Assuming your rag.py is in the same directory
|
3 |
-
import os
|
4 |
-
import time
|
5 |
-
|
6 |
-
# --- Configuration ---
|
7 |
-
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
8 |
-
if not GOOGLE_API_KEY:
|
9 |
-
raise ValueError("GOOGLE_API_KEY environment variable not set. Please set it before running the app.")
|
10 |
-
|
11 |
-
COLLECTION_NAME = os.getenv("COLLECTION_NAME", "ca_documents")
|
12 |
-
|
13 |
-
# --- Initialize RAG System ---
|
14 |
-
try:
|
15 |
-
rag = RAG(GOOGLE_API_KEY, COLLECTION_NAME)
|
16 |
-
except Exception as e:
|
17 |
-
print(f"Fatal Error initializing RAG system: {e}")
|
18 |
-
raise
|
19 |
-
|
20 |
-
# --- Initial Chat State ---
|
21 |
-
welcome_message = {
|
22 |
-
"role": "assistant",
|
23 |
-
"content": """
|
24 |
-
π **Welcome to your CA Study Assistant!**
|
25 |
-
"""
|
26 |
-
}
|
27 |
-
initial_chat_history = [welcome_message]
|
28 |
-
|
29 |
-
# --- Core Functions ---
|
30 |
-
def upload_file(file):
|
31 |
-
if file is None:
|
32 |
-
return """<div class="message-bubble error">
|
33 |
-
<p><strong>β οΈ Please select a file.</strong></p>
|
34 |
-
<p>Supported formats: PDF, DOCX, TXT.</p>
|
35 |
-
</div>"""
|
36 |
-
try:
|
37 |
-
start_time = time.time()
|
38 |
-
file_path = file.name
|
39 |
-
success = rag.upload_document(file_path)
|
40 |
-
duration = time.time() - start_time
|
41 |
-
if success:
|
42 |
-
return f"""<div class=\"message-bubble success\">
|
43 |
-
<p><strong>β
Success!</strong></p>
|
44 |
-
<p><em>{os.path.basename(file_path)}</em> uploaded.</p>
|
45 |
-
<p><small>Processed in {duration:.2f}s</small></p>
|
46 |
-
</div>"""
|
47 |
-
else:
|
48 |
-
return f"""<div class=\"message-bubble error\"><p><strong>β Upload Failed.</strong></p><p>Please ensure <em>{os.path.basename(file_path)}</em> is a valid file.</p></div>"""
|
49 |
-
except Exception as e:
|
50 |
-
return f"""<div class=\"message-bubble error\"><p><strong>β An Error Occurred</strong></p><p><small>{str(e)}</small></p></div>"""
|
51 |
-
|
52 |
-
def chat_with_docs(message: str, history: list):
|
53 |
-
if not message or not message.strip():
|
54 |
-
return "", history
|
55 |
-
history.append({"role": "user", "content": message})
|
56 |
-
answer = rag.ask_question(message)
|
57 |
-
history.append({"role": "assistant", "content": answer})
|
58 |
-
return "", history
|
59 |
-
|
60 |
-
def clear_chat():
|
61 |
-
return initial_chat_history, ""
|
62 |
-
|
63 |
-
# --- Load Custom CSS ---
|
64 |
-
with open("style.css") as f:
|
65 |
-
css = f.read()
|
66 |
-
|
67 |
-
# --- Gradio App Layout ---
|
68 |
-
with gr.Blocks(css=css, title="CA Study Assistant", theme=gr.themes.Base(), mode="auto") as app:
|
69 |
-
with gr.Column(elem_id="app-container"):
|
70 |
-
|
71 |
-
gr.HTML("""
|
72 |
-
<div id="header">
|
73 |
-
<h1>CA Study Assistant</h1>
|
74 |
-
<p>The smartest way to study your materials.</p>
|
75 |
-
</div>
|
76 |
-
""")
|
77 |
-
|
78 |
-
with gr.Tabs(elem_classes="tab-nav"):
|
79 |
-
with gr.TabItem("π¬ Ask Questions", id="chat_tab"):
|
80 |
-
chatbot = gr.Chatbot(
|
81 |
-
value=initial_chat_history,
|
82 |
-
elem_id="chat-history",
|
83 |
-
show_label=False,
|
84 |
-
type="messages",
|
85 |
-
bubble_full_width=False,
|
86 |
-
)
|
87 |
-
with gr.Row(elem_id="chat-input-container"):
|
88 |
-
with gr.Column(scale=10):
|
89 |
-
chat_input = gr.Textbox(
|
90 |
-
placeholder="Ask anything about your documents...",
|
91 |
-
show_label=False, elem_id="chat-input", lines=1, max_lines=5,
|
92 |
-
)
|
93 |
-
with gr.Column(scale=1, min_width=60):
|
94 |
-
send_btn = gr.Button('β€', elem_id="send-btn")
|
95 |
-
clear_btn = gr.Button("ποΈ Clear", elem_id="clear-btn")
|
96 |
-
clear_btn.click(fn=clear_chat, outputs=[chatbot, chat_input])
|
97 |
-
|
98 |
-
with gr.TabItem("π Upload Documents", id="upload_tab"):
|
99 |
-
with gr.Column(elem_id="upload-area"):
|
100 |
-
gr.HTML("""
|
101 |
-
<div id="upload-icon">
|
102 |
-
<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="17 8 12 3 7 8"></polyline><line x1="12" y1="3" x2="12" y2="15"></line></svg>
|
103 |
-
</div>
|
104 |
-
<h3>Upload Your Study Materials</h3>
|
105 |
-
<p>Drag & drop or click below to select a file.</p>
|
106 |
-
<div style="margin-top: 0.5rem;">
|
107 |
-
<span style="background:#c7d2fe;color:#5046e5;padding:0.3rem 0.75rem;border-radius:999px;margin-right:0.5rem;">PDF</span>
|
108 |
-
<span style="background:#c7d2fe;color:#5046e5;padding:0.3rem 0.75rem;border-radius:999px;margin-right:0.5rem;">DOCX</span>
|
109 |
-
<span style="background:#c7d2fe;color:#5046e5;padding:0.3rem 0.75rem;border-radius:999px;">TXT</span>
|
110 |
-
</div>
|
111 |
-
""")
|
112 |
-
upload_input = gr.File(
|
113 |
-
label="", file_count="single", file_types=[".pdf", ".docx", ".txt"],
|
114 |
-
type="filepath",
|
115 |
-
)
|
116 |
-
upload_output = gr.HTML(elem_id="upload-output")
|
117 |
-
|
118 |
-
upload_input.upload(
|
119 |
-
fn=upload_file, inputs=[upload_input], outputs=[upload_output],
|
120 |
-
api_name="upload_document"
|
121 |
-
)
|
122 |
-
send_btn.click(
|
123 |
-
fn=chat_with_docs, inputs=[chat_input, chatbot], outputs=[chat_input, chatbot],
|
124 |
-
)
|
125 |
-
chat_input.submit(
|
126 |
-
fn=chat_with_docs, inputs=[chat_input, chatbot], outputs=[chat_input, chatbot],
|
127 |
-
)
|
128 |
-
|
129 |
-
if __name__ == "__main__":
|
130 |
-
app.launch(share=True, debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|