Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import threading
|
| 3 |
+
import time
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from report_generator import generate_csv_report, generate_xlsx_report, generate_pdf_report
|
| 6 |
+
|
| 7 |
+
def background_heartbeat():
|
| 8 |
+
"""A continuous background task that logs a heartbeat every 60 seconds."""
|
| 9 |
+
while True:
|
| 10 |
+
print(f"[{datetime.now()}] Heartbeat: background task active.")
|
| 11 |
+
time.sleep(60)
|
| 12 |
+
|
| 13 |
+
def process_command(user_input, chat_history):
|
| 14 |
+
"""
|
| 15 |
+
Process the user command and perform tasks accordingly.
|
| 16 |
+
|
| 17 |
+
Commands:
|
| 18 |
+
- If the text includes 'csv', generate a CSV report.
|
| 19 |
+
- If the text includes 'xlsx', generate an XLSX report.
|
| 20 |
+
- If the text includes 'pdf', generate a PDF report.
|
| 21 |
+
- Otherwise, echo the user's input with a friendly response.
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
- Updated chat history (list of tuples).
|
| 25 |
+
- A list of generated file paths (if any).
|
| 26 |
+
"""
|
| 27 |
+
response = ""
|
| 28 |
+
file_links = []
|
| 29 |
+
lower_input = user_input.lower()
|
| 30 |
+
if "csv" in lower_input:
|
| 31 |
+
filename = generate_csv_report()
|
| 32 |
+
response = "CSV report generated."
|
| 33 |
+
file_links.append(filename)
|
| 34 |
+
elif "xlsx" in lower_input:
|
| 35 |
+
filename = generate_xlsx_report()
|
| 36 |
+
response = "XLSX report generated."
|
| 37 |
+
file_links.append(filename)
|
| 38 |
+
elif "pdf" in lower_input:
|
| 39 |
+
filename = generate_pdf_report()
|
| 40 |
+
response = "PDF report generated."
|
| 41 |
+
file_links.append(filename)
|
| 42 |
+
else:
|
| 43 |
+
response = f"You said: {user_input}. How can I help further?"
|
| 44 |
+
|
| 45 |
+
chat_history.append((user_input, response))
|
| 46 |
+
return chat_history, file_links
|
| 47 |
+
|
| 48 |
+
def chat_interface(user_input, history):
|
| 49 |
+
"""
|
| 50 |
+
The main function that gets called when the user submits a command.
|
| 51 |
+
It updates the conversation history and returns any generated files.
|
| 52 |
+
"""
|
| 53 |
+
chat_history, file_links = process_command(user_input, history)
|
| 54 |
+
# Return updated conversation, the generated file(s), and update the state.
|
| 55 |
+
return chat_history, file_links, chat_history
|
| 56 |
+
|
| 57 |
+
def main():
|
| 58 |
+
# Start the background heartbeat thread (daemonized so it exits when the main app stops)
|
| 59 |
+
heartbeat_thread = threading.Thread(target=background_heartbeat, daemon=True)
|
| 60 |
+
heartbeat_thread.start()
|
| 61 |
+
|
| 62 |
+
with gr.Blocks() as demo:
|
| 63 |
+
gr.Markdown("# Interactive Chatbot and Report Generator")
|
| 64 |
+
gr.Markdown(
|
| 65 |
+
"Type commands into the textbox below. For example, try: "
|
| 66 |
+
"`generate csv report`, `generate xlsx report`, or `generate pdf report`."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
chatbot = gr.Chatbot(label="Chatbot Conversation")
|
| 71 |
+
with gr.Row():
|
| 72 |
+
txt = gr.Textbox(label="Enter Command", placeholder="Type your command here...")
|
| 73 |
+
with gr.Row():
|
| 74 |
+
# This component will display generated files as downloadable links.
|
| 75 |
+
file_output = gr.File(label="Generated Files", file_count="multiple")
|
| 76 |
+
|
| 77 |
+
# A hidden state to store conversation history.
|
| 78 |
+
state = gr.State([])
|
| 79 |
+
|
| 80 |
+
# When the user submits a command, update the chatbot, file output, and state.
|
| 81 |
+
txt.submit(
|
| 82 |
+
chat_interface,
|
| 83 |
+
inputs=[txt, state],
|
| 84 |
+
outputs=[chatbot, file_output, state]
|
| 85 |
+
).then(lambda _: "", None, txt)
|
| 86 |
+
|
| 87 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
main()
|