radames commited on
Commit
ebf4f9e
1 Parent(s): 71c165d

copy svelte flask starter

Browse files
.gitignore ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .DS_Store
2
+ .env
3
+ .flaskenv
4
+ *.pyc
5
+ *.pyo
6
+ env/
7
+ venv/
8
+ .venv/
9
+ env*
10
+ dist/
11
+ build/
12
+ *.egg
13
+ *.egg-info/
14
+ _mailinglist
15
+ .tox/
16
+ .cache/
17
+ .pytest_cache/
18
+ .idea/
19
+ docs/_build/
20
+ .vscode
21
+
22
+ # Coverage reports
23
+ htmlcov/
24
+ .coverage
25
+ .coverage.*
26
+ *,cover
27
+ venv
Makefile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ install-node:
2
+ ./install-node.sh
3
+ build-client:
4
+ cd client && npm install && npm run build && pwd && rm -r ../static && cp -r dist/ ../static/
5
+ build-client-dev:
6
+ cd client && npm install && NODE_ENV=development npm run build && pwd && rm -r ../static && cp -r dist/ ../static/
7
+ dev:
8
+ cd client && npm run dev
9
+ run:
10
+ FLASK_ENV=development python app.py
11
+ build-all: install-node build-client run
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🔥
4
  colorFrom: red
5
  colorTo: green
6
  sdk: gradio
7
- app_file: app.py
8
  pinned: false
9
  ---
10
 
 
4
  colorFrom: red
5
  colorTo: green
6
  sdk: gradio
7
+ app_file: main.py
8
  pinned: false
9
  ---
10
 
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ import os
4
+ from dotenv import load_dotenv
5
+ from transformers import pipeline
6
+ import feedparser
7
+
8
+ nyt_homepage_rss = "https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml"
9
+
10
+ load_dotenv()
11
+ # Load Setiment Classifier
12
+ sentiment_analysis = pipeline(
13
+ "sentiment-analysis", model="siebert/sentiment-roberta-large-english")
14
+ app = Flask(__name__, static_url_path='/static')
15
+ CORS(app)
16
+
17
+
18
+ @app.route('/')
19
+ def index():
20
+ return app.send_static_file('index.html')
21
+
22
+
23
+ @app.route('/news')
24
+ def get_news():
25
+ nyt_homepage = get_nytimes()
26
+ # filter only titles for sentiment analysis
27
+ titles = [entry.title for entry in nyt_homepage]
28
+ # run sentiment analysis on titles
29
+ predictions = [sentiment_analysis(sentence) for sentence in titles]
30
+ # parse Negative and Positive, normalize to -1 to 1
31
+ predictions = [-prediction[0]['score'] if prediction[0]['label'] ==
32
+ 'NEGATIVE' else prediction[0]['score'] for prediction in predictions]
33
+ # merge rss data with predictions
34
+ output = [{**entry, 'sentiment': prediction}
35
+ for entry, prediction in zip(nyt_homepage, predictions)]
36
+ # send back json
37
+ return jsonify(output)
38
+
39
+
40
+ @app.route('/predict', methods=['POST'])
41
+ def predict():
42
+ # get data from POST
43
+ if request.method == 'POST':
44
+ # get current news
45
+ # get post body data
46
+ data = request.get_json()
47
+ if data.get('sentences') is None:
48
+ return jsonify({'error': 'No text provided'})
49
+ # get post expeceted to be under {'sentences': ['text': '...']}
50
+ sentences = data.get('sentences')
51
+ # prencit sentiments
52
+ predictions = [sentiment_analysis(sentence) for sentence in sentences]
53
+ # parse Negative and Positive, normalize to -1 to 1
54
+ predictions = [-prediction[0]['score'] if prediction[0]['label'] ==
55
+ 'NEGATIVE' else prediction[0]['score'] for prediction in predictions]
56
+ output = [dict(sentence=sentence, sentiment=prediction)
57
+ for sentence, prediction in zip(sentences, predictions)]
58
+ # send back json
59
+ return jsonify(output)
60
+
61
+
62
+ def get_nytimes():
63
+ feed = feedparser.parse(nyt_homepage_rss)
64
+ return feed.entries
65
+
66
+
67
+ if __name__ == '__main__':
68
+ app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
client/.eslintrc.cjs ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ module.exports = {
2
+ root: true,
3
+ extends: ['eslint:recommended', 'prettier'],
4
+ plugins: ['svelte3'],
5
+ overrides: [{ files: ['*.svelte'], processor: 'svelte3/svelte3' }],
6
+ parserOptions: {
7
+ sourceType: 'module',
8
+ ecmaVersion: 2020
9
+ },
10
+ env: {
11
+ browser: true,
12
+ es2017: true,
13
+ node: true
14
+ }
15
+ };
client/.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ .DS_Store
2
+ node_modules
3
+ /build
4
+ /.svelte-kit
5
+ /package
6
+ .env
7
+ .env.*
8
+ !.env.example
client/.npmrc ADDED
@@ -0,0 +1 @@
 
 
1
+ engine-strict=true
client/.prettierrc ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "useTabs": true,
3
+ "singleQuote": true,
4
+ "trailingComma": "none",
5
+ "printWidth": 100
6
+ }
client/README.md ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # create-svelte
2
+
3
+ Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).
4
+
5
+ ## Creating a project
6
+
7
+ If you're seeing this, you've probably already done this step. Congrats!
8
+
9
+ ```bash
10
+ # create a new project in the current directory
11
+ npm init svelte@next
12
+
13
+ # create a new project in my-app
14
+ npm init svelte@next my-app
15
+ ```
16
+
17
+ > Note: the `@next` is temporary
18
+
19
+ ## Developing
20
+
21
+ Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
22
+
23
+ ```bash
24
+ npm run dev
25
+
26
+ # or start the server and open the app in a new browser tab
27
+ npm run dev -- --open
28
+ ```
29
+
30
+ ## Building
31
+
32
+ To create a production version of your app:
33
+
34
+ ```bash
35
+ npm run build
36
+ ```
37
+
38
+ You can preview the production build with `npm run preview`.
39
+
40
+ > To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
client/jsconfig.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": ".",
4
+ "paths": {
5
+ "$lib": ["src/lib"],
6
+ "$lib/*": ["src/lib/*"]
7
+ }
8
+ },
9
+ "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
10
+ }
client/package-lock.json ADDED
The diff for this file is too large to render. See raw diff
 
client/package.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "client",
3
+ "version": "0.0.1",
4
+ "scripts": {
5
+ "dev": "svelte-kit dev",
6
+ "build": "svelte-kit build",
7
+ "package": "svelte-kit package",
8
+ "preview": "svelte-kit preview",
9
+ "lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
10
+ "format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
11
+ },
12
+ "devDependencies": {
13
+ "@sveltejs/adapter-static": "^1.0.0-next.28",
14
+ "@sveltejs/kit": "next",
15
+ "eslint": "^7.32.0",
16
+ "eslint-config-prettier": "^8.3.0",
17
+ "eslint-plugin-svelte3": "^3.2.1",
18
+ "prettier": "^2.4.1",
19
+ "prettier-plugin-svelte": "^2.4.0",
20
+ "svelte": "^3.44.0"
21
+ },
22
+ "type": "module"
23
+ }
client/src/app.html ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <base href="" />
5
+ <meta charset="utf-8" />
6
+ <meta name="description" content="" />
7
+ <link rel="icon" href="%svelte.assets%/favicon.png" />
8
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
9
+ %svelte.head%
10
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"></script>
11
+ </head>
12
+ <body>
13
+ <div>%svelte.body%</div>
14
+ </body>
15
+ </html>
client/src/routes/index.svelte ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script>
2
+ const fecthPredictions = fetch('news').then((d) => d.json());
3
+ </script>
4
+
5
+ <h1>The New York Times Homepage</h1>
6
+
7
+ {#await fecthPredictions}
8
+ <p>Loading and running sentiment analysis on the latest news...</p>
9
+ {:then data}
10
+ <ul>
11
+ {#each data as entry, i}
12
+ <li>
13
+ <a target="_blank" href={entry.link}>
14
+ {i + 1}: {entry.title}
15
+ </a>
16
+ </li>
17
+ {/each}
18
+ </ul>
19
+ {:catch error}
20
+ <p>An error occurred!</p>
21
+ {/await}
client/static/favicon.png ADDED
client/svelte.config.js ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import adapter from '@sveltejs/adapter-static'
2
+ const dev = process.env.NODE_ENV === 'development';
3
+
4
+ /** @type {import('@sveltejs/kit').Config} */
5
+ const config = {
6
+ kit: {
7
+ paths: {
8
+ base: dev ? '/static' : '/gradioiframe/Rad/NYTimes-homepage-rearranged/static',
9
+ assets: '',
10
+ },
11
+ appDir: '_app',
12
+ adapter: adapter({
13
+ pages: 'dist',
14
+ assets: 'dist',
15
+ fallback: null,
16
+ precompress: false
17
+ })
18
+ }
19
+ };
20
+
21
+ export default config;
install-node.sh ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
2
+ export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
3
+ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
4
+ nvm install --lts
5
+ node --version
6
+ npm --version
7
+ which node
8
+ which npm
9
+ command ln -s "$NVM_BIN/node" /home/user/.local/bin/node
10
+ command ln -s "$NVM_BIN/npm" /home/user/.local/bin/npm
main.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ import subprocess
2
+
3
+ subprocess.run(["make", "build-all"], shell=False)
packages.txt ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ feedparser==6.0.8
2
+ Flask==2.0.2
3
+ flask_cors==3.0.10
4
+ python-dotenv==0.19.2
5
+ transformers==4.16.2
6
+ torch
static/.gitignore ADDED
File without changes