Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Import chatbot logic
|
| 6 |
+
from chatbot import Chatbot
|
| 7 |
+
|
| 8 |
+
# Load environment variables from .env file
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
|
| 13 |
+
# Initialize the chatbot
|
| 14 |
+
chatbot = Chatbot()
|
| 15 |
+
|
| 16 |
+
@app.route('/')
|
| 17 |
+
def home():
|
| 18 |
+
return render_template('index.html')
|
| 19 |
+
|
| 20 |
+
@app.route('/ask', methods=['POST'])
|
| 21 |
+
def ask():
|
| 22 |
+
# Get the user's message
|
| 23 |
+
user_message = request.form['message']
|
| 24 |
+
|
| 25 |
+
# Get the response from the chatbot
|
| 26 |
+
response = chatbot.get_response(user_message)
|
| 27 |
+
|
| 28 |
+
return jsonify({'response': response})
|
| 29 |
+
|
| 30 |
+
if __name__ == '__main__':
|
| 31 |
+
app.run(debug=True)
|
| 32 |
+
from .math import solve_math_problem
|
| 33 |
+
from .english import correct_grammar
|
| 34 |
+
from .science import explain_science_concept
|
| 35 |
+
from .python import solve_python_code
|
| 36 |
+
|
| 37 |
+
class Chatbot:
|
| 38 |
+
def __init__(self):
|
| 39 |
+
pass
|
| 40 |
+
|
| 41 |
+
def get_response(self, user_input):
|
| 42 |
+
# Determine the subject and return the appropriate response
|
| 43 |
+
if 'math' in user_input.lower():
|
| 44 |
+
return solve_math_problem(user_input)
|
| 45 |
+
elif 'grammar' in user_input.lower() or 'english' in user_input.lower():
|
| 46 |
+
return correct_grammar(user_input)
|
| 47 |
+
elif 'science' in user_input.lower():
|
| 48 |
+
return explain_science_concept(user_input)
|
| 49 |
+
elif 'python' in user_input.lower() or 'code' in user_input.lower():
|
| 50 |
+
return solve_python_code(user_input)
|
| 51 |
+
else:
|
| 52 |
+
return "Sorry, I don't understand. Can you please clarify your question?"
|
| 53 |
+
from sympy import symbols, Eq, solve
|
| 54 |
+
|
| 55 |
+
def solve_math_problem(user_input):
|
| 56 |
+
# For simplicity, assume the user asks a solvable equation like 'solve x + 5 = 10'
|
| 57 |
+
try:
|
| 58 |
+
# Create symbolic variable x
|
| 59 |
+
x = symbols('x')
|
| 60 |
+
|
| 61 |
+
# Parse and solve the equation
|
| 62 |
+
equation = Eq(eval(user_input), 0) # Simplified eval, you can make it more complex
|
| 63 |
+
solution = solve(equation, x)
|
| 64 |
+
|
| 65 |
+
return f"The solution to {user_input} is {solution}"
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return "Sorry, I couldn't solve the math problem. Please check your input."
|
| 68 |
+
from textblob import TextBlob
|
| 69 |
+
|
| 70 |
+
def correct_grammar(user_input):
|
| 71 |
+
blob = TextBlob(user_input)
|
| 72 |
+
corrected = blob.correct()
|
| 73 |
+
|
| 74 |
+
return f"Corrected sentence: {corrected}"
|
| 75 |
+
def explain_science_concept(user_input):
|
| 76 |
+
# Placeholder logic, can be expanded to integrate an external knowledge base or API
|
| 77 |
+
return f"Here's an explanation about {user_input}. [This can be extended further with a real knowledge base]."
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|