Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,8 @@ def extract_text_from_file(file):
|
|
21 |
elif ext.endswith((".png", ".jpg", ".jpeg", ".bmp")):
|
22 |
return "Image uploaded. You can ask about it."
|
23 |
elif ext.endswith((".txt", ".py", ".json", ".html", ".md", ".css", ".js")):
|
24 |
-
with open(file.name, encoding="utf-8", errors="ignore") as f:
|
|
|
25 |
except Exception as e:
|
26 |
return f"File extraction failed: {e}"
|
27 |
|
@@ -59,16 +60,23 @@ def chat_response(history, message):
|
|
59 |
except Exception as e:
|
60 |
return f"Groq response failed: {e}"
|
61 |
|
|
|
|
|
|
|
62 |
def process_input(msg, file, audio, history):
|
63 |
file_text = extract_text_from_file(file) if file else ""
|
64 |
audio_text = transcribe_audio(audio) if audio else ""
|
65 |
full_input = "\n".join([i for i in [msg, file_text, audio_text] if i])
|
|
|
66 |
if not full_input.strip():
|
67 |
-
return history, gr.Textbox.update(value=""), None, None, gr.Textbox.update(value="No input provided.")
|
|
|
68 |
reply = chat_response(history, full_input)
|
69 |
history.append(("user", msg.strip() or file_text or audio_text))
|
70 |
history.append(("assistant", reply))
|
71 |
-
|
|
|
|
|
72 |
|
73 |
def save_chat_history(history):
|
74 |
try:
|
@@ -97,7 +105,7 @@ def load_selected_chat(filename):
|
|
97 |
history.append((role.strip(), content.strip()))
|
98 |
return history, gr.Textbox.update(value=f"Chat '{filename}' loaded.")
|
99 |
|
100 |
-
def clear_chat(): return [], gr.Textbox.update(value="Chat cleared. Start fresh!")
|
101 |
|
102 |
custom_css = """
|
103 |
.gradio-container { max-width: 1200px !important; margin: auto !important; }
|
@@ -107,7 +115,7 @@ select { appearance: auto !important; }
|
|
107 |
with gr.Blocks(css=custom_css, title="Neobot - Chatbot") as demo:
|
108 |
gr.Markdown("# 🤖 Neobot - Advanced AI Chatbot")
|
109 |
|
110 |
-
chatbot = gr.Chatbot(label="Chat History", height=500, show_copy_button=True)
|
111 |
message_input = gr.Textbox(placeholder="Type your message or upload a file...", scale=4)
|
112 |
send_button = gr.Button("Send 📤", scale=1)
|
113 |
|
@@ -124,21 +132,23 @@ with gr.Blocks(css=custom_css, title="Neobot - Chatbot") as demo:
|
|
124 |
status_message = gr.Textbox(label="Status", interactive=False)
|
125 |
chat_history = gr.State([])
|
126 |
|
127 |
-
send_button.click(process_input, [message_input, file_upload, audio_upload, chat_history],
|
128 |
-
|
129 |
|
130 |
-
message_input.submit(process_input, [message_input, file_upload, audio_upload, chat_history],
|
131 |
-
|
132 |
|
133 |
-
save_button.click(save_chat_history, [chat_history], [status_message])\
|
134 |
.then(lambda: gr.Dropdown.update(choices=get_saved_chats()), None, [saved_chats_dropdown])
|
135 |
|
136 |
-
clear_button.click(clear_chat, None, [chat_history, status_message])\
|
137 |
.then(lambda: [], None, [chatbot])
|
138 |
|
139 |
refresh_button.click(lambda: gr.Dropdown.update(choices=get_saved_chats()), None, [saved_chats_dropdown])
|
140 |
|
141 |
-
load_button.click(load_selected_chat, [saved_chats_dropdown], [chat_history, status_message])\
|
142 |
-
.then(lambda h: h, [chat_history], [chatbot])
|
143 |
|
144 |
-
|
|
|
|
|
|
21 |
elif ext.endswith((".png", ".jpg", ".jpeg", ".bmp")):
|
22 |
return "Image uploaded. You can ask about it."
|
23 |
elif ext.endswith((".txt", ".py", ".json", ".html", ".md", ".css", ".js")):
|
24 |
+
with open(file.name, encoding="utf-8", errors="ignore") as f:
|
25 |
+
return f.read()
|
26 |
except Exception as e:
|
27 |
return f"File extraction failed: {e}"
|
28 |
|
|
|
60 |
except Exception as e:
|
61 |
return f"Groq response failed: {e}"
|
62 |
|
63 |
+
def format_for_chatbot(history):
|
64 |
+
return [{"role": role, "content": content} for role, content in history]
|
65 |
+
|
66 |
def process_input(msg, file, audio, history):
|
67 |
file_text = extract_text_from_file(file) if file else ""
|
68 |
audio_text = transcribe_audio(audio) if audio else ""
|
69 |
full_input = "\n".join([i for i in [msg, file_text, audio_text] if i])
|
70 |
+
|
71 |
if not full_input.strip():
|
72 |
+
return history, gr.Textbox.update(value=""), None, None, gr.Textbox.update(value="⚠️ No input provided."), gr.update(value=format_for_chatbot(history))
|
73 |
+
|
74 |
reply = chat_response(history, full_input)
|
75 |
history.append(("user", msg.strip() or file_text or audio_text))
|
76 |
history.append(("assistant", reply))
|
77 |
+
|
78 |
+
status_text = "✅ Response generated." if "failed" not in reply.lower() else f"⚠️ Error: {reply}"
|
79 |
+
return history, gr.Textbox.update(value=""), None, None, gr.Textbox.update(value=status_text), gr.update(value=format_for_chatbot(history))
|
80 |
|
81 |
def save_chat_history(history):
|
82 |
try:
|
|
|
105 |
history.append((role.strip(), content.strip()))
|
106 |
return history, gr.Textbox.update(value=f"Chat '{filename}' loaded.")
|
107 |
|
108 |
+
def clear_chat(): return [], gr.Textbox.update(value="🧹 Chat cleared. Start fresh!")
|
109 |
|
110 |
custom_css = """
|
111 |
.gradio-container { max-width: 1200px !important; margin: auto !important; }
|
|
|
115 |
with gr.Blocks(css=custom_css, title="Neobot - Chatbot") as demo:
|
116 |
gr.Markdown("# 🤖 Neobot - Advanced AI Chatbot")
|
117 |
|
118 |
+
chatbot = gr.Chatbot(label="Chat History", height=500, show_copy_button=True, type="messages")
|
119 |
message_input = gr.Textbox(placeholder="Type your message or upload a file...", scale=4)
|
120 |
send_button = gr.Button("Send 📤", scale=1)
|
121 |
|
|
|
132 |
status_message = gr.Textbox(label="Status", interactive=False)
|
133 |
chat_history = gr.State([])
|
134 |
|
135 |
+
send_button.click(process_input, [message_input, file_upload, audio_upload, chat_history],
|
136 |
+
[chat_history, message_input, file_upload, audio_upload, status_message, chatbot])
|
137 |
|
138 |
+
message_input.submit(process_input, [message_input, file_upload, audio_upload, chat_history],
|
139 |
+
[chat_history, message_input, file_upload, audio_upload, status_message, chatbot])
|
140 |
|
141 |
+
save_button.click(save_chat_history, [chat_history], [status_message]) \
|
142 |
.then(lambda: gr.Dropdown.update(choices=get_saved_chats()), None, [saved_chats_dropdown])
|
143 |
|
144 |
+
clear_button.click(clear_chat, None, [chat_history, status_message]) \
|
145 |
.then(lambda: [], None, [chatbot])
|
146 |
|
147 |
refresh_button.click(lambda: gr.Dropdown.update(choices=get_saved_chats()), None, [saved_chats_dropdown])
|
148 |
|
149 |
+
load_button.click(load_selected_chat, [saved_chats_dropdown], [chat_history, status_message]) \
|
150 |
+
.then(lambda h: format_for_chatbot(h), [chat_history], [chatbot])
|
151 |
|
152 |
+
# ✅ Launch your app and confirm it's running
|
153 |
+
demo.launch()
|
154 |
+
print("✅ Neobot is now live! Listening for messages...")
|