-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
90 lines (74 loc) · 2.96 KB
/
app.py
File metadata and controls
90 lines (74 loc) · 2.96 KB
1
2
3
4
5
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File: D:\Tutor-AI\app.py
import os
import re
from flask import (
Flask, render_template, request, session,
redirect, url_for, flash
)
from dotenv import load_dotenv
load_dotenv()
from agents.mentor_agent import MentorAgent
from tools.email_tool import EmailTool
from tools.ocr_tool import OCRTool
app = Flask(__name__)
app.secret_key = os.urandom(24)
mentor_agent = MentorAgent()
email_tool = EmailTool()
ocr_tool = OCRTool()
def simple_format_markdown(text):
text = re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', text)
text = re.sub(r'\*(.+?)\*', r'<em>\1</em>', text)
text = re.sub(r'`(.+?)`', r'<code>\1</code>', text)
return text.replace('\n', '<br>')
@app.route('/', methods=['GET', 'POST'])
def index():
conv = session.get('conversation', [])
if request.method == 'POST':
# 1) Handle image upload for OCR
img = request.files.get('image')
if img and img.filename:
extracted = ocr_tool.extract_text(img).strip()
# Show the extracted text as user message
conv.append({'role': 'user',
'message': simple_format_markdown(f"Extracted text from image:\n{extracted}")})
# Classify subject + answer
answer, _ = mentor_agent.handle_question(extracted)
conv.append({'role': 'assistant',
'message': simple_format_markdown(answer)})
session['conversation'] = conv
return redirect(url_for('index'))
# 2) Otherwise, normal chat/question or YouTube URL
user_input = request.form.get('question')
if user_input:
conv.append({'role': 'user',
'message': simple_format_markdown(user_input)})
answer, _ = mentor_agent.handle_question(user_input)
conv.append({'role': 'assistant',
'message': simple_format_markdown(answer)})
session['conversation'] = conv
return redirect(url_for('index'))
return render_template('index.html', conversation=conv)
@app.route('/send_email', methods=['POST'])
def send_email():
recipient = request.form.get('recipient')
if not recipient:
flash("Please enter a valid email address.", "error")
return redirect(url_for('index'))
conv = session.get('conversation', [])
if not conv:
flash("No conversation to send.", "error")
return redirect(url_for('index'))
# Build full HTML
html_parts = []
for msg in conv:
prefix = "You: " if msg['role']=='user' else "Tutor: "
html_parts.append(f"<strong>{prefix}</strong>{msg['message']}<br>")
full_html = "\n".join(html_parts)
try:
email_tool.send_pdf_email(recipient, full_html)
flash(f"📧 PDF of entire chat sent to {recipient}!", "success")
except Exception as e:
flash(f"Failed to send email: {e}", "error")
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(debug=True)