Spaces:
Sleeping
Sleeping
Ali Abdullah commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -231,7 +231,7 @@ Perfect for beginners and experienced programmers alike!"""
|
|
| 231 |
|
| 232 |
Step 1: Choose Your Path
|
| 233 |
• Complete Beginner? Start with Python Fundamentals
|
| 234 |
-
|
| 235 |
• Want to Specialize? Pick Machine Learning or AI track
|
| 236 |
|
| 237 |
Step 2: Learning Format
|
|
@@ -656,17 +656,17 @@ def create_chat_interface():
|
|
| 656 |
setTyping(true);
|
| 657 |
|
| 658 |
try {
|
| 659 |
-
const response = await fetch(
|
| 660 |
method: 'POST',
|
| 661 |
headers: { 'Content-Type': 'application/json' },
|
| 662 |
-
body: JSON.stringify({ message })
|
| 663 |
});
|
| 664 |
|
| 665 |
const data = await response.json();
|
| 666 |
|
| 667 |
await new Promise(resolve => setTimeout(resolve, 1200));
|
| 668 |
|
| 669 |
-
addMessage(data.
|
| 670 |
setConnected(true);
|
| 671 |
|
| 672 |
} catch (error) {
|
|
@@ -806,18 +806,8 @@ def create_chat_interface():
|
|
| 806 |
</html>
|
| 807 |
"""
|
| 808 |
|
| 809 |
-
def chat_response(message
|
| 810 |
-
if not message.strip():
|
| 811 |
-
return history, ""
|
| 812 |
-
|
| 813 |
-
if history is None:
|
| 814 |
-
history = []
|
| 815 |
-
|
| 816 |
-
history = history + [[message, None]]
|
| 817 |
-
|
| 818 |
try:
|
| 819 |
-
time.sleep(1)
|
| 820 |
-
|
| 821 |
if retriever:
|
| 822 |
docs = retriever.get_relevant_documents(message)
|
| 823 |
context = "\n\n".join([doc.page_content for doc in docs[:3]])
|
|
@@ -825,28 +815,12 @@ def chat_response(message, history):
|
|
| 825 |
context = "I'm an AI assistant for Atomcamp, a data science education platform."
|
| 826 |
|
| 827 |
response = generate_response_with_groq(message, context)
|
| 828 |
-
|
| 829 |
-
history[-1][1] = response
|
| 830 |
-
|
| 831 |
-
except Exception as e:
|
| 832 |
-
history[-1][1] = f"Sorry, I encountered an error: {str(e)}. Please try again."
|
| 833 |
-
|
| 834 |
-
return history, ""
|
| 835 |
-
|
| 836 |
-
def api_chat(message):
|
| 837 |
-
try:
|
| 838 |
-
if retriever:
|
| 839 |
-
docs = retriever.get_relevant_documents(message)
|
| 840 |
-
context = "\n\n".join([doc.page_content for doc in docs[:3]])
|
| 841 |
-
else:
|
| 842 |
-
context = "I'm an AI assistant for Atomcamp."
|
| 843 |
-
|
| 844 |
-
response = generate_response_with_groq(message, context)
|
| 845 |
-
return {"response": response}
|
| 846 |
|
| 847 |
except Exception as e:
|
| 848 |
-
return
|
| 849 |
|
|
|
|
| 850 |
with gr.Blocks(
|
| 851 |
title="atomcamp AI Chatbot",
|
| 852 |
css="""
|
|
@@ -861,14 +835,22 @@ with gr.Blocks(
|
|
| 861 |
"""
|
| 862 |
) as demo:
|
| 863 |
|
|
|
|
| 864 |
with gr.Row(visible=False):
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
|
|
|
|
| 869 |
gr.HTML(create_chat_interface())
|
| 870 |
-
|
| 871 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 872 |
|
| 873 |
if __name__ == "__main__":
|
| 874 |
demo.launch(
|
|
|
|
| 231 |
|
| 232 |
Step 1: Choose Your Path
|
| 233 |
• Complete Beginner? Start with Python Fundamentals
|
| 234 |
+
• Some Programming Experience? Jump to Data Science Essentials
|
| 235 |
• Want to Specialize? Pick Machine Learning or AI track
|
| 236 |
|
| 237 |
Step 2: Learning Format
|
|
|
|
| 656 |
setTyping(true);
|
| 657 |
|
| 658 |
try {
|
| 659 |
+
const response = await fetch('/predict', {
|
| 660 |
method: 'POST',
|
| 661 |
headers: { 'Content-Type': 'application/json' },
|
| 662 |
+
body: JSON.stringify({ data: [message] })
|
| 663 |
});
|
| 664 |
|
| 665 |
const data = await response.json();
|
| 666 |
|
| 667 |
await new Promise(resolve => setTimeout(resolve, 1200));
|
| 668 |
|
| 669 |
+
addMessage(data.data[0] || 'Sorry, I could not generate a response.', 'bot');
|
| 670 |
setConnected(true);
|
| 671 |
|
| 672 |
} catch (error) {
|
|
|
|
| 806 |
</html>
|
| 807 |
"""
|
| 808 |
|
| 809 |
+
def chat_response(message):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 810 |
try:
|
|
|
|
|
|
|
| 811 |
if retriever:
|
| 812 |
docs = retriever.get_relevant_documents(message)
|
| 813 |
context = "\n\n".join([doc.page_content for doc in docs[:3]])
|
|
|
|
| 815 |
context = "I'm an AI assistant for Atomcamp, a data science education platform."
|
| 816 |
|
| 817 |
response = generate_response_with_groq(message, context)
|
| 818 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
|
| 820 |
except Exception as e:
|
| 821 |
+
return f"Sorry, I encountered an error: {str(e)}. Please try again."
|
| 822 |
|
| 823 |
+
# Create the Gradio interface
|
| 824 |
with gr.Blocks(
|
| 825 |
title="atomcamp AI Chatbot",
|
| 826 |
css="""
|
|
|
|
| 835 |
"""
|
| 836 |
) as demo:
|
| 837 |
|
| 838 |
+
# Hidden Gradio components for API functionality
|
| 839 |
with gr.Row(visible=False):
|
| 840 |
+
input_text = gr.Textbox()
|
| 841 |
+
output_text = gr.Textbox()
|
| 842 |
+
submit_btn = gr.Button()
|
| 843 |
|
| 844 |
+
# Custom HTML interface
|
| 845 |
gr.HTML(create_chat_interface())
|
| 846 |
+
|
| 847 |
+
# Set up the API endpoint
|
| 848 |
+
submit_btn.click(
|
| 849 |
+
fn=chat_response,
|
| 850 |
+
inputs=input_text,
|
| 851 |
+
outputs=output_text,
|
| 852 |
+
api_name="predict"
|
| 853 |
+
)
|
| 854 |
|
| 855 |
if __name__ == "__main__":
|
| 856 |
demo.launch(
|