Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Added Google Docs Support #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ Model_training/AnswerAwareQG/fine_tuned_t5_tokenizer_aaqg_2
backend/models
backend/tokenizers
backend/sample_input.py
extension/pdfjs-3.9.179-dist
extension/pdfjs-3.9.179-dist
backend/service_account_key.json
61 changes: 61 additions & 0 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from flask import Flask, jsonify, request
from flask_cors import CORS
from google.oauth2 import service_account
from googleapiclient.discovery import build
import re

# Replace the path below with the path to your service account key JSON file
SERVICE_ACCOUNT_FILE = './service_account_key.json'

# Scopes required for accessing Google Docs API
SCOPES = ['https://www.googleapis.com/auth/documents.readonly']

app = Flask(__name__)
CORS(app)

# Initialize the Google Docs API client
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)

docs_service = build('docs', 'v1', credentials=credentials)


def extract_document_id(url):
"""
Extracts the Google Docs document ID from a given URL.
"""
match = re.search(r'/document/d/([^/]+)', url)
if match:
return match.group(1)
return None


@app.route('/get_content', methods=['POST'])
def get_content():
try:
data = request.get_json()
document_url = data.get('document_url')
if not document_url:
return jsonify({'error': 'Document URL is required'}), 400

document_id = extract_document_id(document_url)
if not document_id:
return jsonify({'error': 'Invalid document URL'}), 400

response = docs_service.documents().get(documentId=document_id).execute()
doc = response.get('body', {})

text = ''
for element in doc.get('content', []):
if 'paragraph' in element:
for p in element['paragraph']['elements']:
if 'textRun' in p:
text += p['textRun']['content']

return jsonify(text.strip())
except Exception as e:
return jsonify({'error': str(e)}), 500


if __name__ == '__main__':
app.run(debug=True)
13 changes: 13 additions & 0 deletions backend/example_service_account_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "your-project-id",
"private_key_id": "your-private-key-id",
"private_key": "-----BEGIN PRIVATE KEY-----\nYOUR_PRIVATE_KEY_HERE\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "your-client-id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/your-client-email%40your-project-id.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
6 changes: 6 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flask
google-auth
google-auth-oauthlib
google-auth-httplib2
google-api-python-client
flask-cors
1 change: 1 addition & 0 deletions extension/html/text_input.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ <h3>Generate QnA</h3>
<input type="file" id="file-upload" accept=".pdf" hidden>
<label for="file-upload" id="upload-label">Upload PDF &#128196</label>
</div>
<textarea id="doc-input" placeholder="Enter URL here"></textarea>
<div class="button-container">
<button id="back-button">Back</button>
<button id="next-button">Next</button>
Expand Down
29 changes: 27 additions & 2 deletions extension/js/text_input.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ document.addEventListener("DOMContentLoaded", function () {
const textInput = document.getElementById("text-input");
const fileInput = document.getElementById("file-upload");
const loadingScreen = document.getElementById("loading-screen");

const docInput = document.getElementById("doc-input")

fileInput.addEventListener("change", async function () {
const file = fileInput.files[0];
Expand All @@ -29,6 +29,11 @@ document.addEventListener("DOMContentLoaded", function () {
});

nextButton.addEventListener("click", async function () {
if(docInput.value!='')
{
content = await fetchDoc(docInput.value)
textInput.value = content
}
loadingScreen.style.display = "flex"
const inputText = textInput.value;

Expand All @@ -51,8 +56,28 @@ document.addEventListener("DOMContentLoaded", function () {
backButton.addEventListener("click", function () {
window.location.href = "../html/index.html";
});

async function fetchDoc(urli) {
const document_url = urli;
const url = 'http://localhost:5000/get_content';

const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ document_url })
});

if (!response.ok) {
throw new Error('Error fetching document content');
}

const data = await response.json();
return data;

}
async function sendToBackend(data, dataType) {

let formData;
let contentType;
formData = JSON.stringify({ 'input_text': data });
Expand Down
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "1.0",
"description": "Generate quizzes with AI-powered questions.",
"permissions": ["activeTab", "storage"],
"host_permissions":["http://127.0.0.1:8000/*"],
"host_permissions":["http://127.0.0.1:8000/*","http://127.0.0.1:5000/*"],
"icons": {
"16": "./assets/aossie_logo.png"
},
Expand Down