Update app.py
Browse files
app.py
CHANGED
|
@@ -48,16 +48,18 @@ def predict(message, history, subject_id, file_input):
|
|
| 48 |
formatted_contents = []
|
| 49 |
if history:
|
| 50 |
for turn in history:
|
| 51 |
-
if turn
|
| 52 |
-
formatted_contents.append(types.Content(role="user", parts=[types.Part.from_text(str(turn
|
| 53 |
-
|
| 54 |
-
formatted_contents.append(types.Content(role="
|
|
|
|
| 55 |
|
| 56 |
current_parts = [types.Part.from_text(message)]
|
| 57 |
|
| 58 |
if file_input is not None:
|
| 59 |
try:
|
| 60 |
-
|
|
|
|
| 61 |
pdf_bytes = f.read()
|
| 62 |
current_parts.append(
|
| 63 |
types.Part.from_bytes(
|
|
@@ -121,7 +123,7 @@ def generate_flashcards(history, subject_id):
|
|
| 121 |
return f"Error generating flashcards: {str(e)}"
|
| 122 |
|
| 123 |
# ── Gradio UI Construction ──
|
| 124 |
-
with gr.Blocks(
|
| 125 |
current_subject_id = gr.State(value="math")
|
| 126 |
|
| 127 |
with gr.Row():
|
|
@@ -173,7 +175,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="slate")
|
|
| 173 |
)
|
| 174 |
|
| 175 |
file_upload = gr.File(label="📂 Upload Study Material (PDF up to 50MB)", file_types=[".pdf"])
|
| 176 |
-
chatbot = gr.Chatbot(height=400
|
| 177 |
|
| 178 |
with gr.Row():
|
| 179 |
msg_input = gr.Textbox(placeholder="Ask anything...", scale=4, show_label=False)
|
|
@@ -192,7 +194,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="slate")
|
|
| 192 |
outputs=[subject_info_banner, suggestion_box, current_subject_id, chatbot]
|
| 193 |
)
|
| 194 |
|
| 195 |
-
# Direct text box binding
|
| 196 |
suggestion_box.select(lambda x: x.value, inputs=[suggestion_box], outputs=[msg_input])
|
| 197 |
|
| 198 |
def handle_audio(audio_path):
|
|
@@ -205,12 +206,10 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="slate")
|
|
| 205 |
if not message or not message.strip():
|
| 206 |
return "", chat_history
|
| 207 |
|
| 208 |
-
# Gradio 6 style history list append
|
| 209 |
chat_history = chat_history or []
|
| 210 |
chat_history.append({"role": "user", "metadata": {"title": ""}, "content": message})
|
| 211 |
chat_history.append({"role": "assistant", "metadata": {"title": ""}, "content": ""})
|
| 212 |
|
| 213 |
-
# Convert gradio history items for raw loop standard if needed
|
| 214 |
raw_history = []
|
| 215 |
for i in range(0, len(chat_history)-2, 2):
|
| 216 |
if i+1 < len(chat_history):
|
|
@@ -245,4 +244,4 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="slate")
|
|
| 245 |
|
| 246 |
if __name__ == "__main__":
|
| 247 |
demo.queue()
|
| 248 |
-
demo.launch()
|
|
|
|
| 48 |
formatted_contents = []
|
| 49 |
if history:
|
| 50 |
for turn in history:
|
| 51 |
+
if isinstance(turn, dict):
|
| 52 |
+
formatted_contents.append(types.Content(role=turn.get("role", "user"), parts=[types.Part.from_text(str(turn.get("content", "")))]))
|
| 53 |
+
elif isinstance(turn, (list, tuple)) and len(turn) >= 2:
|
| 54 |
+
if turn[0]: formatted_contents.append(types.Content(role="user", parts=[types.Part.from_text(str(turn[0]))]))
|
| 55 |
+
if turn[1]: formatted_contents.append(types.Content(role="model", parts=[types.Part.from_text(str(turn[1]))]))
|
| 56 |
|
| 57 |
current_parts = [types.Part.from_text(message)]
|
| 58 |
|
| 59 |
if file_input is not None:
|
| 60 |
try:
|
| 61 |
+
filepath = file_input if isinstance(file_input, str) else getattr(file_input, "name", str(file_input))
|
| 62 |
+
with open(filepath, "rb") as f:
|
| 63 |
pdf_bytes = f.read()
|
| 64 |
current_parts.append(
|
| 65 |
types.Part.from_bytes(
|
|
|
|
| 123 |
return f"Error generating flashcards: {str(e)}"
|
| 124 |
|
| 125 |
# ── Gradio UI Construction ──
|
| 126 |
+
with gr.Blocks() as demo:
|
| 127 |
current_subject_id = gr.State(value="math")
|
| 128 |
|
| 129 |
with gr.Row():
|
|
|
|
| 175 |
)
|
| 176 |
|
| 177 |
file_upload = gr.File(label="📂 Upload Study Material (PDF up to 50MB)", file_types=[".pdf"])
|
| 178 |
+
chatbot = gr.Chatbot(height=400)
|
| 179 |
|
| 180 |
with gr.Row():
|
| 181 |
msg_input = gr.Textbox(placeholder="Ask anything...", scale=4, show_label=False)
|
|
|
|
| 194 |
outputs=[subject_info_banner, suggestion_box, current_subject_id, chatbot]
|
| 195 |
)
|
| 196 |
|
|
|
|
| 197 |
suggestion_box.select(lambda x: x.value, inputs=[suggestion_box], outputs=[msg_input])
|
| 198 |
|
| 199 |
def handle_audio(audio_path):
|
|
|
|
| 206 |
if not message or not message.strip():
|
| 207 |
return "", chat_history
|
| 208 |
|
|
|
|
| 209 |
chat_history = chat_history or []
|
| 210 |
chat_history.append({"role": "user", "metadata": {"title": ""}, "content": message})
|
| 211 |
chat_history.append({"role": "assistant", "metadata": {"title": ""}, "content": ""})
|
| 212 |
|
|
|
|
| 213 |
raw_history = []
|
| 214 |
for i in range(0, len(chat_history)-2, 2):
|
| 215 |
if i+1 < len(chat_history):
|
|
|
|
| 244 |
|
| 245 |
if __name__ == "__main__":
|
| 246 |
demo.queue()
|
| 247 |
+
demo.launch(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="slate"))
|