Update app.py
Browse files
app.py
CHANGED
@@ -1,165 +1,201 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import logging
|
3 |
-
|
4 |
-
|
5 |
-
from
|
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 |
-
TRANSLATIONS[lang]
|
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 |
-
with gr.Row():
|
111 |
-
with gr.Column():
|
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 |
-
TRANSLATIONS[lang]["
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import logging
|
3 |
+
import PyPDF2
|
4 |
+
|
5 |
+
from config import SHARE_GRADIO_WITH_PUBLIC_URL
|
6 |
+
from chains import qa_chain, summarization_chain
|
7 |
+
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
|
10 |
+
# Translation dictionary
|
11 |
+
TRANSLATIONS = {
|
12 |
+
"en": {
|
13 |
+
"title": "# 📚 Study Buddy: AI Learning Assistant",
|
14 |
+
"subtitle": "## 🤖 A smart, user-friendly chatbot for students!",
|
15 |
+
"summary_subtitle": "## 📄 Upload Notes for Summarization",
|
16 |
+
"chat_input_label": "Type your question here:",
|
17 |
+
"chat_placeholder": "e.g., Explain Newton's laws",
|
18 |
+
"chat_button_label": "Get Answer",
|
19 |
+
"summary_button_label": "Summarize Notes",
|
20 |
+
"upload_file_label": "Upload .txt or .pdf file",
|
21 |
+
"summary_output_label": "Summary",
|
22 |
+
"language_label": "Language / Langue",
|
23 |
+
"ai_response_label": "AI Response"
|
24 |
+
},
|
25 |
+
"fr": {
|
26 |
+
"title": "# 📚 Study Buddy: Assistant d'apprentissage IA",
|
27 |
+
"subtitle": "## 🤖 Un chatbot intelligent et convivial pour les étudiants!",
|
28 |
+
"summary_subtitle": "## 📄 Subir notas para resumir",
|
29 |
+
"chat_input_label": "Tapez votre question ici:",
|
30 |
+
"chat_placeholder": "ex : Expliquez les lois de Newton",
|
31 |
+
"chat_button_label": "Obtenir une réponse",
|
32 |
+
"summary_button_label": "Résumer les notes",
|
33 |
+
"upload_file_label": "Téléchargez un fichier .txt ou .pdf",
|
34 |
+
"summary_output_label": "Résumé",
|
35 |
+
"language_label": "Langue / Language",
|
36 |
+
"ai_response_label": "Réponse de l'IA"
|
37 |
+
}
|
38 |
+
}
|
39 |
+
|
40 |
+
# Function to process user queries
|
41 |
+
def chatbot_response(user_input, lang):
|
42 |
+
try:
|
43 |
+
response_output = qa_chain.invoke({"question": user_input})
|
44 |
+
response = response_output.content
|
45 |
+
logger.info("chatbot_response completed")
|
46 |
+
print("> chatbot_response completed")
|
47 |
+
return response
|
48 |
+
except Exception as e:
|
49 |
+
msg = f"Error : {e}"
|
50 |
+
logger.exception(msg)
|
51 |
+
print(msg)
|
52 |
+
return TRANSLATIONS[lang].get("error_message", "Sorry, an error occurred while processing your request.")
|
53 |
+
|
54 |
+
# Function to summarize notes
|
55 |
+
def summarize_pdf(pdf, lang):
|
56 |
+
try:
|
57 |
+
with open(pdf, "rb") as file:
|
58 |
+
reader = PyPDF2.PdfReader(file)
|
59 |
+
page = reader.pages[0] # Get the first page
|
60 |
+
text = page.extract_text()
|
61 |
+
print(text)
|
62 |
+
summary = summarize_text(text, lang)
|
63 |
+
logger.info("summarize_pdf completed")
|
64 |
+
print("> summarize_pdf completed")
|
65 |
+
return summary
|
66 |
+
except Exception as e:
|
67 |
+
msg = f"Error : {e}"
|
68 |
+
logger.exception(msg)
|
69 |
+
print(msg)
|
70 |
+
return TRANSLATIONS[lang].get("error_message", "Sorry, an error occurred while summarizing your notes.")
|
71 |
+
|
72 |
+
# Function to summarize notes
|
73 |
+
def summarize_text(text, lang):
|
74 |
+
try:
|
75 |
+
summary_output = summarization_chain.invoke({"document_text": text})
|
76 |
+
print(summary_output)
|
77 |
+
summary = summary_output.content
|
78 |
+
logger.info("summarize_text completed")
|
79 |
+
print("> summarize_text completed")
|
80 |
+
return summary
|
81 |
+
except Exception as e:
|
82 |
+
msg = f"Error : {e}"
|
83 |
+
logger.exception(msg)
|
84 |
+
print(msg)
|
85 |
+
return TRANSLATIONS[lang].get("error_message", "Sorry, an error occurred while summarizing your notes.")
|
86 |
+
|
87 |
+
# Function to update UI labels dynamically
|
88 |
+
def update_language(lang):
|
89 |
+
return (
|
90 |
+
TRANSLATIONS[lang]["title"],
|
91 |
+
TRANSLATIONS[lang]["subtitle"],
|
92 |
+
TRANSLATIONS[lang]["chat_input_label"],
|
93 |
+
TRANSLATIONS[lang]["chat_placeholder"],
|
94 |
+
TRANSLATIONS[lang]["chat_button_label"],
|
95 |
+
TRANSLATIONS[lang]["upload_file_label"],
|
96 |
+
TRANSLATIONS[lang]["summary_button_label"],
|
97 |
+
TRANSLATIONS[lang]["summary_output_label"],
|
98 |
+
TRANSLATIONS[lang]["ai_response_label"]
|
99 |
+
)
|
100 |
+
|
101 |
+
# Gradio UI
|
102 |
+
def create_interface():
|
103 |
+
with gr.Blocks(css="body { font-family: sans-serif; background-color: #f9f9f9; }") as study_buddy:
|
104 |
+
|
105 |
+
# Default to English
|
106 |
+
lang = "en"
|
107 |
+
|
108 |
+
title = gr.Markdown(f"{TRANSLATIONS[lang]['title']}")
|
109 |
+
|
110 |
+
with gr.Row():
|
111 |
+
with gr.Column():
|
112 |
+
gr.Markdown("", height=4)
|
113 |
+
|
114 |
+
language = gr.Radio(
|
115 |
+
choices=["en", "fr"],
|
116 |
+
value=lang,
|
117 |
+
label=TRANSLATIONS[lang]["language_label"]
|
118 |
+
)
|
119 |
+
|
120 |
+
gr.Markdown("", height=4)
|
121 |
+
|
122 |
+
subtitle = gr.Markdown(f"{TRANSLATIONS[lang]['subtitle']}")
|
123 |
+
|
124 |
+
chat_input = gr.Textbox(
|
125 |
+
# label=TRANSLATIONS[lang]["chat_input_label"]
|
126 |
+
label= "Type your question here: / Tapez votre question ici:",
|
127 |
+
lines=4,
|
128 |
+
placeholder=TRANSLATIONS[lang]["chat_placeholder"]
|
129 |
+
)
|
130 |
+
|
131 |
+
with gr.Column():
|
132 |
+
gr.Markdown("", height=4)
|
133 |
+
summary_subtitle = gr.Markdown(f"{TRANSLATIONS[lang]['summary_subtitle']}")
|
134 |
+
file_input = gr.File(label="File / Fichier",file_types=[".pdf", ".txt"])
|
135 |
+
file_label = gr.Markdown(TRANSLATIONS[lang]["upload_file_label"]) # Separate label
|
136 |
+
|
137 |
+
with gr.Row():
|
138 |
+
with gr.Column():
|
139 |
+
|
140 |
+
chat_button = gr.Button(TRANSLATIONS[lang]["chat_button_label"], variant="primary")
|
141 |
+
chat_output = gr.Textbox(
|
142 |
+
# label=TRANSLATIONS[lang]["ai_response_label"],
|
143 |
+
label = "AI Response / Réponse de l'IA",
|
144 |
+
lines=5, interactive=True
|
145 |
+
)
|
146 |
+
|
147 |
+
# Bind chatbot response function
|
148 |
+
chat_button.click(
|
149 |
+
chatbot_response,
|
150 |
+
inputs=[chat_input, language],
|
151 |
+
outputs=chat_output
|
152 |
+
)
|
153 |
+
|
154 |
+
with gr.Column():
|
155 |
+
|
156 |
+
summary_button = gr.Button(TRANSLATIONS[lang]["summary_button_label"], variant="primary")
|
157 |
+
summary_output = gr.Textbox(
|
158 |
+
# label=TRANSLATIONS[lang]["summary_output_label"],
|
159 |
+
label="Summary / Résumé",
|
160 |
+
lines=5,
|
161 |
+
interactive=True
|
162 |
+
)
|
163 |
+
|
164 |
+
# Bind summarization function
|
165 |
+
summary_button.click(
|
166 |
+
summarize_pdf,
|
167 |
+
inputs=[file_input, language],
|
168 |
+
outputs=summary_output
|
169 |
+
)
|
170 |
+
|
171 |
+
# Update labels dynamically when the language changes
|
172 |
+
def update_labels(lang):
|
173 |
+
return (
|
174 |
+
TRANSLATIONS[lang]["title"],
|
175 |
+
TRANSLATIONS[lang]["subtitle"],
|
176 |
+
TRANSLATIONS[lang]["summary_subtitle"],
|
177 |
+
TRANSLATIONS[lang]["chat_input_label"],
|
178 |
+
TRANSLATIONS[lang]["chat_placeholder"],
|
179 |
+
TRANSLATIONS[lang]["chat_button_label"],
|
180 |
+
TRANSLATIONS[lang]["summary_button_label"],
|
181 |
+
TRANSLATIONS[lang]["summary_output_label"],
|
182 |
+
TRANSLATIONS[lang]["ai_response_label"],
|
183 |
+
TRANSLATIONS[lang]["upload_file_label"]
|
184 |
+
)
|
185 |
+
|
186 |
+
language.change(
|
187 |
+
update_labels,
|
188 |
+
inputs=[language],
|
189 |
+
outputs=[
|
190 |
+
title, subtitle, summary_subtitle,
|
191 |
+
chat_input, chat_input,
|
192 |
+
chat_button,
|
193 |
+
summary_button, summary_output, chat_output, file_label # Update file label separately
|
194 |
+
]
|
195 |
+
)
|
196 |
+
|
197 |
+
return study_buddy
|
198 |
+
|
199 |
+
if __name__ == "__main__":
|
200 |
+
study_buddy = create_interface()
|
201 |
+
study_buddy.launch(share=SHARE_GRADIO_WITH_PUBLIC_URL)
|