-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
139 lines (115 loc) · 4.24 KB
/
web.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
from flask import Flask, render_template, request, send_file
import pdfplumber
import docx
import google.generativeai as genai
from werkzeug.utils import secure_filename
from fpdf import FPDF
# Set your API key
os.environ["GOOGLE_API_KEY"] = "Your api key"
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
model = genai.GenerativeModel("models/gemini-1.5-pro")
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads/'
app.config['RESULTS_FOLDER'] = 'results/'
app.config['ALLOWED_EXTENSIONS'] = {'pdf', 'txt', 'docx'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
def extract_text_from_file(file_path):
ext = file_path.rsplit('.', 1)[1].lower()
if ext == 'pdf':
with pdfplumber.open(file_path) as pdf:
text = ''.join([page.extract_text() for page in pdf.pages])
return text
elif ext == 'docx':
doc = docx.Document(file_path)
text = ' '.join([para.text for para in doc.paragraphs])
return text
elif ext == 'txt':
with open(file_path, 'r') as file:
return file.read()
return None
def Question_mcqs_generator(input_text, num_questions):
prompt = f"""
Generate {num_questions} MCQs with explanations based on the following conversation text:
'{input_text}'
Format:
## MCQ
Question: [question]
A) [option A]
B) [option B]
C) [option C]
D) [option D]
Correct Answer: [correct option]
Explanation: [short explanation]
"""
response = model.generate_content(prompt).text.strip()
return response
def generate_article(input_text):
prompt = f"""
Generate an article based on the following conversation text :
'{input_text}'
Format:
Article Title: [Your Article Title Here]
Introduction
[Introduction paragraph]
Main Body
[your main body with section headings]
Conclusion
[your conclusion]
"""
article = model.generate_content(prompt).text.strip()
return article
def save_mcqs_to_file(mcqs, filename):
results_path = os.path.join(app.config['RESULTS_FOLDER'], filename)
with open(results_path, 'w') as f:
f.write(mcqs)
return results_path
def create_pdf(mcqs, filename):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
for mcq in mcqs.split("## MCQ"):
if mcq.strip():
pdf.multi_cell(0, 10, mcq.strip())
pdf.ln(5)
pdf_path = os.path.join(app.config['RESULTS_FOLDER'], filename)
pdf.output(pdf_path)
return pdf_path
@app.route('/')
def index():
return render_template('index.html')
@app.route('/generate', methods=['POST'])
def generate():
if 'file' not in request.files:
return "No file part"
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
text = extract_text_from_file(file_path)
if text:
num_questions = int(request.form['num_questions'])
mcqs = Question_mcqs_generator(text, num_questions)
article = generate_article(text)
txt_filename = f"generated_mcqs_{filename.rsplit('.', 1)[0]}.txt"
pdf_filename = f"generated_mcqs_{filename.rsplit('.', 1)[0]}.pdf"
save_mcqs_to_file(mcqs, txt_filename)
create_pdf(mcqs, pdf_filename)
return render_template(
'results.html', # Change this to your results template
mcqs=mcqs,
article=article,
txt_filename=txt_filename,
pdf_filename=pdf_filename
)
return "Invalid file format"
@app.route('/download/<filename>')
def download_file(filename):
file_path = os.path.join(app.config['RESULTS_FOLDER'], filename)
return send_file(file_path, as_attachment=True)
if __name__ == "__main__":
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['RESULTS_FOLDER'], exist_ok=True)
app.run(debug=True)