-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
119 lines (99 loc) · 3.15 KB
/
main.py
File metadata and controls
119 lines (99 loc) · 3.15 KB
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
import gradio as gr
from bot_src.bot import CuriousPeerBot
from bot_src.utils import read_pdf, save_chat_history
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize bot
bot = CuriousPeerBot()
def process_file(file):
"""Process uploaded PDF file"""
if file is None:
return "Please upload a PDF file."
filename = os.path.basename(file.name)
bot.set_current_file(filename)
text = read_pdf(file.name)
tldr = bot.generate_tldr(text)
return f"TLDR of {filename}:\n\n{tldr}"
def chat(message, history):
"""Handle chat interaction"""
bot_response = bot.chat(message)
# messages 형식으로 변환
if history is None:
history = []
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": bot_response})
return "", history
def save_history():
"""Save chat history"""
if not bot.get_chat_history():
return "No chat history to save."
output_file = save_chat_history(bot.current_file, bot.get_chat_history())
return f"Chat history saved to: {output_file}"
# Create Gradio interface
with gr.Blocks(
theme=gr.themes.Soft(),
css=".gradio-container {font-family: Arial, sans-serif}"
) as interface:
gr.Markdown("# 📚 Curious Peer Bot")
gr.Markdown("Upload an academic article and let's discuss it together!")
with gr.Row():
with gr.Column(scale=2):
file_input = gr.File(
label="Upload Academic Article (PDF)",
file_types=[".pdf"]
)
process_btn = gr.Button("📄 Process Article", variant="primary")
tldr_output = gr.Textbox(
label="Article Summary",
lines=10,
interactive=False
)
with gr.Row():
chatbot = gr.Chatbot(
label="Discussion",
height=400,
bubble_full_width=False,
show_copy_button=True,
type="messages"
)
with gr.Row():
msg_input = gr.Textbox(
label="Your message",
placeholder="Type your thoughts or questions here... (Ctrl+Enter to send)",
lines=3,
show_label=False
)
with gr.Row():
with gr.Column(scale=1):
send_btn = gr.Button("🚀 Send", variant="primary")
with gr.Column(scale=1):
save_btn = gr.Button("💾 Save Chat History", variant="secondary")
save_status = gr.Textbox(label="Save Status", interactive=False)
# Event handlers
process_btn.click(
fn=process_file,
inputs=[file_input],
outputs=[tldr_output]
)
msg_input.submit(
fn=chat,
inputs=[msg_input, chatbot],
outputs=[msg_input, chatbot]
)
send_btn.click(
fn=chat,
inputs=[msg_input, chatbot],
outputs=[msg_input, chatbot]
)
save_btn.click(
fn=save_history,
outputs=[save_status]
)
if __name__ == "__main__":
interface.launch(
server_port=7860,
share=True,
debug=True
)