Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Firebase separation #77

Open
wants to merge 141 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
141 commits
Select commit Hold shift + click to select a range
15c0923
Created the netlify.toml file and the file to handle the metadata
Maria-Aidarus Nov 22, 2023
ecc7a01
Updated the handleMetadata.js
Maria-Aidarus Nov 22, 2023
126cad3
Merge pull request #1 from Maria-Aidarus/netlify
Maria-Aidarus Nov 22, 2023
7c97630
created an html form that can take in the url and api key
Maria-Aidarus Nov 22, 2023
9681f2c
Merge pull request #2 from Maria-Aidarus/netlify
Maria-Aidarus Nov 22, 2023
e482963
moved the form.html to another directory
Maria-Aidarus Nov 22, 2023
88e27db
Merge pull request #3 from Maria-Aidarus/netlify
Maria-Aidarus Nov 22, 2023
4a8c0e5
updated the css on the form.html page
Maria-Aidarus Nov 22, 2023
312b2e6
updated the css on the form.html page
Maria-Aidarus Nov 22, 2023
4e18092
updated the css on the form.html page
Maria-Aidarus Nov 22, 2023
7e5cb70
updated the css on the form.html page
Maria-Aidarus Nov 22, 2023
b27909a
updated the css on the form.html page
Maria-Aidarus Nov 22, 2023
3e2020a
updated the css on the form.html page
Maria-Aidarus Nov 22, 2023
0ea53c2
updated the css on the form.html page
Maria-Aidarus Nov 22, 2023
4f8dddf
updated the css on the form.html page
Maria-Aidarus Nov 22, 2023
f68436d
added functionality for fetchContentFromURL(); added some notes for p…
hjoad Nov 23, 2023
c0cb0a9
accidentally removed a comma
hjoad Nov 23, 2023
338e155
Merge pull request #4 from Maria-Aidarus/web-scraping
hjoad Nov 23, 2023
a713eae
added node-fetch to the package.json file
Maria-Aidarus Nov 23, 2023
c3bd6f8
added openai to package.json
hjoad Nov 23, 2023
002bba1
added openai to the package.json file
Maria-Aidarus Nov 23, 2023
5952faa
Merge branch 'main' of https://github.com/Maria-Aidarus/learndb
hjoad Nov 23, 2023
6ab782c
added openai to package.json file
hjoad Nov 23, 2023
ae8186a
removed openai from the handleMetadata file
Maria-Aidarus Nov 23, 2023
104ca57
testing if the imports are broken
hjoad Nov 23, 2023
1905bd7
removed all imports
Maria-Aidarus Nov 23, 2023
fbf55af
updated the return for testing
hjoad Nov 23, 2023
510bad2
cried
Maria-Aidarus Nov 23, 2023
8cc6e36
reverted the handleMetadata file
Maria-Aidarus Nov 23, 2023
2cdcfef
return the fetchedContent instead of formattedResponse
hjoad Nov 23, 2023
eb2fcf4
added back in the node-fetch import
hjoad Nov 23, 2023
0101732
changed format of import
hjoad Nov 23, 2023
e90b328
changed node-fetch version in package.json
hjoad Nov 23, 2023
2195b4e
changed version of node-fetch back; import https instead of node-fetch
hjoad Nov 23, 2023
5888f7b
changed node-fetch version with uninstall and reinstalling, added nod…
hjoad Nov 23, 2023
d9418bd
trying to use https import for webscraping instead of node-fetch
hjoad Nov 23, 2023
734d989
added simplyContent functionality (not tested yet); changed the retur…
hjoad Nov 24, 2023
a4da9cb
changed the format back
hjoad Nov 24, 2023
c0ea5e0
testing simplifiedContent function
hjoad Nov 24, 2023
3132902
added a link to remove links
hjoad Nov 24, 2023
3dfa222
added stuff to get rid of formatting from html
hjoad Nov 24, 2023
70e817e
improving simplifyContent()
hjoad Nov 24, 2023
ec711e2
trying to add node-fetch again
hjoad Nov 24, 2023
238963a
changed node-fetch version type, trying to make it work
hjoad Nov 24, 2023
b7e4ba6
trying with axios instead of node-fetch
hjoad Nov 24, 2023
7ac1609
testing without axios
hjoad Nov 24, 2023
71516c9
added in axios again
hjoad Nov 24, 2023
29a2116
trying node-fetch with .default
hjoad Nov 24, 2023
ced2867
removing node-fetch import
hjoad Nov 24, 2023
b367e0c
adding node-fetch back in
hjoad Nov 24, 2023
4b9ea1b
removing node-fetch import
hjoad Nov 24, 2023
2a8de7a
adding node-fetch back in
hjoad Nov 24, 2023
fe353ad
removing node-fetch
hjoad Nov 24, 2023
81175f2
adding node-fetch
hjoad Nov 24, 2023
39549d1
removing node-fetch
hjoad Nov 24, 2023
bfc2480
downgraded node-fetch
hjoad Nov 24, 2023
72c94b6
added npm install to netlify.toml and added node-fetch back in
hjoad Nov 24, 2023
ed67407
changed netlify.toml
hjoad Nov 24, 2023
ccb2ae1
changed netlify.toml
hjoad Nov 24, 2023
ccf5ced
changed netlify.toml
hjoad Nov 24, 2023
90e0f02
request node-fetch
hjoad Nov 24, 2023
ef73d65
axios
hjoad Nov 24, 2023
e591ed3
removed imports
hjoad Nov 24, 2023
17f6bbc
not summarizing
hjoad Nov 24, 2023
feb3843
was returning the wrong thing
hjoad Nov 24, 2023
c29406a
added node-fetch
hjoad Nov 24, 2023
4cb262b
changed netlify.toml
hjoad Nov 24, 2023
2c4bf05
changed netlify.toml
hjoad Nov 24, 2023
f0064a6
removed default
hjoad Nov 24, 2023
34da189
changed netlify.toml
hjoad Nov 24, 2023
2847364
import openai
hjoad Nov 24, 2023
e302e41
added new package.json
hjoad Nov 24, 2023
fe364b9
changed openai import
hjoad Nov 24, 2023
aae2faa
changed package.json
hjoad Nov 24, 2023
9f08219
tried to fix the openai import
Maria-Aidarus Nov 24, 2023
4dcfe72
moved everything into the same folder
Maria-Aidarus Nov 24, 2023
9e84ec5
moved everything into the same folder
Maria-Aidarus Nov 24, 2023
9ab0543
updated the deployment settings
Maria-Aidarus Nov 24, 2023
32c4b15
updated the deployment settings
Maria-Aidarus Nov 24, 2023
ac2d27e
updated the deployment settings
Maria-Aidarus Nov 24, 2023
369e633
added an install script
Maria-Aidarus Nov 24, 2023
9825e20
added an install script
Maria-Aidarus Nov 24, 2023
021922c
removed the install script
Maria-Aidarus Nov 24, 2023
f0b3468
removed the install script
Maria-Aidarus Nov 24, 2023
b73bc63
removed the install script
Maria-Aidarus Nov 24, 2023
7da53fb
removed the install script
Maria-Aidarus Nov 24, 2023
7ff1cde
inshallah
Maria-Aidarus Nov 24, 2023
1f0f249
added openai to netlify.toml
Maria-Aidarus Nov 24, 2023
4026cb5
inshallah this works
Maria-Aidarus Nov 24, 2023
e68e858
inshallah this works
Maria-Aidarus Nov 24, 2023
6e097f8
moved the form.html file
Maria-Aidarus Nov 24, 2023
cd5fa89
moved the form.html file
Maria-Aidarus Nov 24, 2023
7e38977
updated the package.json file
Maria-Aidarus Nov 24, 2023
96a2bfb
trying node-fetch now
Maria-Aidarus Nov 24, 2023
e34158b
updated the function, uses node-fetch now
Maria-Aidarus Nov 24, 2023
5dc0645
trying node-fetch now
Maria-Aidarus Nov 24, 2023
2bad128
checking if fetchedContent() works correctly
hjoad Nov 24, 2023
535ccfa
modified fetchContentFromURL() to use node-fetch now that import is w…
hjoad Nov 24, 2023
60c6bc5
modified catch error block to give error message
hjoad Nov 24, 2023
9cd0345
downgraded node-fetch version
hjoad Nov 24, 2023
a603edd
updated package.json to type module, updated import to match commonjs…
hjoad Nov 24, 2023
b866b6e
updated the response to test simplifyContent()
hjoad Nov 24, 2023
4a5bb87
modified simplifyContent()
hjoad Nov 24, 2023
d8d5b0d
changed back to previous version
hjoad Nov 24, 2023
9cdf9e6
changed openai import
hjoad Nov 24, 2023
259e454
modified simplifyContent()
hjoad Nov 24, 2023
cf1833e
modified simplifyContent to make it cover more html
hjoad Nov 24, 2023
73443e4
modified the simplifyContent function to retain punctuation to make i…
hjoad Nov 24, 2023
a9a0d26
added in special handling for headings in simplifyContent()
hjoad Nov 24, 2023
ff46e8f
checking if it works without stringify
hjoad Nov 24, 2023
d235492
changed back cause nothing different
hjoad Nov 24, 2023
c4b0159
new import: he
hjoad Nov 24, 2023
950a4ae
changed import format
hjoad Nov 24, 2023
6e6d44f
removed he as it is not really needed, just helpful, but gave runtime…
hjoad Nov 24, 2023
ab40600
added he back for testing
hjoad Nov 24, 2023
45e4a69
changed import type
hjoad Nov 24, 2023
5cbb0f2
fixed syntax error
hjoad Nov 24, 2023
0056258
changed to use https again in fetchContentFromURL()
hjoad Nov 24, 2023
e6b9cfc
using node-fetch again cause apparently that wasnt the issue
hjoad Nov 24, 2023
5145b0f
trying another way to simplify
hjoad Nov 24, 2023
c46f1b0
edited comments
hjoad Nov 24, 2023
bb86825
removed the overflow option for the form
Maria-Aidarus Nov 26, 2023
e864cdb
updated the paramters for the gpt function
Maria-Aidarus Nov 27, 2023
d83b03c
removed the functionality from the gpt analysis function
Maria-Aidarus Nov 28, 2023
c00c8f0
added the try/catch block back
Maria-Aidarus Nov 28, 2023
4cf2d5e
changed the import
Maria-Aidarus Nov 28, 2023
586030a
removed the import
Maria-Aidarus Nov 28, 2023
c6857eb
chnaged the syntax of the import
Maria-Aidarus Nov 28, 2023
2993b45
added the ai model functionality
Maria-Aidarus Nov 28, 2023
92e01d6
chnaged the import again
Maria-Aidarus Nov 28, 2023
edf8f0e
updated the package.json file
Maria-Aidarus Nov 28, 2023
3a16890
updated the package.json file
Maria-Aidarus Nov 28, 2023
18e4309
chnaged the formatting
Maria-Aidarus Nov 28, 2023
5478b77
changed the structure of the imports
Maria-Aidarus Nov 29, 2023
801cabd
changed the structure of the imports
Maria-Aidarus Nov 29, 2023
326f4bd
commented out the try/catch block in the gpt analysis function
Maria-Aidarus Nov 29, 2023
8c3506b
fixed the firebase deployment service
Maria-Aidarus Nov 29, 2023
4af057d
updated the dependencies
Maria-Aidarus Nov 29, 2023
99a7e67
installed required modules for firebase
rama0711 Nov 30, 2023
57bd22e
initialized the firebase project
rama0711 Nov 30, 2023
1f54d05
set up the function for firebase
rama0711 Nov 30, 2023
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
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "learbdb-cmuq"
}
}
20 changes: 20 additions & 0 deletions .github/workflows/firebase-hosting-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on merge
'on':
push:
branches:
- main
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm ci && npm run build
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_LEARBDB_CMUQ }}'
channelId: live
projectId: learbdb-cmuq
17 changes: 17 additions & 0 deletions .github/workflows/firebase-hosting-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on PR
'on': pull_request
jobs:
build_and_preview:
if: '${{ github.event.pull_request.head.repo.full_name == github.repository }}'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm ci && npm run build
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_LEARBDB_CMUQ }}'
projectId: learbdb-cmuq
37 changes: 37 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
]
}
],
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
"emulators": {
"functions": {
"port": 5001
},
"ui": {
"enabled": true
},
"singleProjectMode": true
}
}
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
146 changes: 146 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
const fetch = require('node-fetch'); // Import for webscraping in fetchContentFromURL()
import { OpenAIApi, Configuration } from 'openai';
// const { Configuration, OpenAIApi } = require('openai');

// Function to fetch content from URL using a web scraping service
async function fetchContentFromURL(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.text();
} catch (error) {
console.error(`Could not fetch content from URL: ${error}`);
throw error;
}
}

function simplifyContent(content) {
// Denote headings with a marker (like '###') and add a line break
content = content.replace(/<h[1-6].*?>(.*?)<\/h[1-6]>/g, '\n### $1\n');
// Remove script and style elements and their content
let simplifiedContent = content.replace(/<script.*?>.*?<\/script>/gms, '');
simplifiedContent = simplifiedContent.replace(/<style.*?>.*?<\/style>/gms, '');
// Remove all remaining HTML tags, leaving the inner text
simplifiedContent = simplifiedContent.replace(/<[^>]+>/g, '');
// Manually replace common HTML entities
simplifiedContent = simplifiedContent
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'");
// Remove inline CSS and JavaScript event handlers
simplifiedContent = simplifiedContent.replace(/style\s*=\s*'.*?'/gi, '');
simplifiedContent = simplifiedContent.replace(/on\w+\s*=\s*".*?"/gi, '');
// Normalize whitespace without removing sentence punctuation
simplifiedContent = simplifiedContent.replace(/\s+/g, ' ').trim();
// Condense multiple line breaks into a single one
simplifiedContent = simplifiedContent.replace(/(\r\n|\r|\n){2,}/g, '\n');
return simplifiedContent;
}


// Placeholder function to perform GPT analysis for media type and topics using Mistral-7b via OpenRouter
async function performGPTAnalysis(simplifiedContent, apiKey) {
// Implement logic to send content to Mistral-7b via OpenRouter for GPT analysis
// Send content and receive GPT analysis response

// this is the code that we tried to use for the GPT Analysis
// try {
// const configuration = new Configuration({
// apiKey: apiKey, // Use the provided API key
// baseURL: "https://openrouter.ai/api/v1" // Your custom API endpoint
// });

// const openai = new OpenAIApi(configuration);

// // Using the specified prompt
// const prompt = `Analyze the following text and provide the media type and key topics: ${simplifiedContent}`;

// const completion = await openai.createCompletion({
// model: "mistralai/mistral-7b-instruct",
// prompt: prompt,
// max_tokens: 150 // Adjust as needed
// });

// //return completion.data.choices[0].text.trim();
// return inferredMediaType;
// } catch (error) {
// console.error('Error with OpenAI completion:', error);
// throw error;
// }
// however, it gives the error below:
// { "error": "Something went wrong", "details": "Configuration is not a constructor" }

// Placeholder code
const inferredMediaType = ["article"];
const extractedTopics = ["topic1", "topic2"];
return { inferredMediaType, extractedTopics };
}

// Placeholder function to map inferred values to predefined formats and topics
function mapInferredValues(mediaType, topics) {
// Implement logic to map inferred media type and topics to predefined formats and topics
// Match inferred values with predefined taxonomy
// Placeholder code
const predefinedMediaType = "Article";
const predefinedTopics = ["Topic 1", "Topic 2"];
return { predefinedMediaType, predefinedTopics };
}

// Placeholder function to format the response
function formatResponse(predefinedMediaType, predefinedTopics) {
// Implement logic to format the extracted metadata into the desired response structure
// Construct the response object
// Placeholder code
const response = {
format: predefinedMediaType,
topics: predefinedTopics,
};
return response;
}

export async function handler(event) {
try {
// Extract URL and API Key from the request body
const { url, apiKey } = JSON.parse(event.body);

// Validate if URL and API Key are present
if (!url || !apiKey) {
return {
statusCode: 400,
body: JSON.stringify({ error: 'URL and API Key are required' }),
};
}

// Step 1: Fetch content from the URL using a web scraping service
const fetchedContent = await fetchContentFromURL(url);

// Step 2: Simplify the fetched content for GPT analysis
const simplifiedContent = simplifyContent(fetchedContent);

// Step 3: Perform GPT analysis for media type and topics
const { inferredMediaType, extractedTopics } = await performGPTAnalysis(simplifiedContent, apiKey);

// Step 4: Map inferred values to predefined formats and topics
const { predefinedMediaType, predefinedTopics } = mapInferredValues(inferredMediaType, extractedTopics);

// Step 5: Format the response
const formattedResponse = formatResponse(predefinedMediaType, predefinedTopics);

// Return the formatted response
return {
statusCode: 200,
// returning the output of the simplifyContent function, to test the function
body: JSON.stringify(simplifiedContent),
};
} catch (error) {
console.error('Error occurred:', error.message);
return {
statusCode: 500,
body: JSON.stringify({ error: 'Something went wrong', details: error.message }),
};
}
}
23 changes: 23 additions & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "18"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^11.8.0",
"firebase-functions": "^4.3.1"
},
"devDependencies": {
"firebase-functions-test": "^3.1.0"
},
"private": true
}
2 changes: 2 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
functions = "netlify/functions/"
Loading