Skip to content

Commit

Permalink
connected front end to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
carelynntsai committed Sep 15, 2019
1 parent e3b6f83 commit 6b61e21
Show file tree
Hide file tree
Showing 1,155 changed files with 45,750 additions and 576 deletions.
152 changes: 0 additions & 152 deletions client/package-lock.json

Large diffs are not rendered by default.

71 changes: 67 additions & 4 deletions client/src/pages/Recording.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ export default class Recording extends Component {

handleListen = (event) => {
if (event.results[0][0].confidence >= 0.85) {
this.setState({transcript: event.results[0][0]})
this.setState({ transcript: event.results[0][0] })
}
}

changeQ = () => {
this.setState({first: true, question: questions[Math.floor(Math.random() * 10)] })
this.setState({ first: true, question: questions[Math.floor(Math.random() * 10)] })
}

restart = () => {
this.setState({transcript: "", first: true, playing: false});
this.setState({ transcript: "", first: true, playing: false });
recognition.stop();
this.recordAction();
}
Expand All @@ -52,7 +52,7 @@ export default class Recording extends Component {
recognition.stop();
console.log("recording has stopped");
}
this.setState({first: false, playing: !isPlaying});
this.setState({ first: false, playing: !isPlaying });
}

analyze = () => {
Expand All @@ -61,6 +61,7 @@ export default class Recording extends Component {

render() {
return (
<<<<<<< HEAD
<div className="main" style={{paddingTop: 100}}>
<div>
<Link to="/">
Expand Down Expand Up @@ -116,6 +117,68 @@ export default class Recording extends Component {
<div>
<p> Don't like the question? Click <span onClick={this.changeQ} style={{textDecoration: "underline", cursor: "pointer"}}>here</span> for a new one </p>
</div>
=======
<div className="main" style={{ paddingTop: 50 }}>
<div>
<img src={logo} width="15%" alt="logo" />
<h2>
" {this.state.question} "
</h2>
</div>
<div style={{ padding: 20 }}>
<img src={this.state.playing === true ? play : pause} alt={this.state.playing ? "recordAction" : "PAUSED"} onClick={this.recording} style={{ cursor: "pointer" }} />
</div>
{this.state.transcript.length === 0 && this.state.first === false && this.state.playing === false && "No audio was found"}
<div style={{ padding: 20 }}>
{this.state.transcript.length === 0 || this.state.playing === true ?
<button className="button" onClick={this.recordAction}>
<h2 style={{ cursor: "pointer" }}> {this.state.playing === true ? "Stop Recording" : "Start Recording"} </h2>
</button> :
<div className="group">
<div className="groupbutton">
<button className="button" onClick={this.recordAction}>
<h2 style={{ cursor: "pointer" }}> Continue </h2>
</button>
</div>
<div className="groupbutton">
<button className="button" onClick={this.recordAction}>
<h2 style={{ cursor: "pointer" }}> Restart </h2>
</button>
</div>
<div className="groupbutton">
<button className="accentButton" onClick={async () => {
const documents = [{
id: '1',
text: this.state.transcript.transcript,
language: "en",
}]

console.log(this.state.transcript.transcript)

const response = await fetch('http://localhost:3001/analyze', {
method: 'POST',
mode: 'cors',
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(documents), // body data type must match "Content-Type" header
})

const json = await response.json()
alert(json.sentiment.documents[0].score*100)
}
} >
<h2 style={{ cursor: "pointer" }}> Analyze </h2>
</button>
</div>
</div>
}
</div>
<div>
<p> Don't like the question? Click <span onClick={this.changeQ} style={{ textDecoration: "underline", cursor: "pointer" }}>here</span> for a new one </p>
</div>
>>>>>>> connected front end to backend
</div>
);
}
Expand Down
48 changes: 48 additions & 0 deletions expressapp/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const CognitiveServicesCredentials = require("@azure/ms-rest-js");
const TextAnalyticsAPIClient = require("@azure/cognitiveservices-textanalytics");

const cors = require('cors')

const express = require('express')
const app = express()
const port = 3001

app.use(cors())
app.use(express.json())

//app.get('/', (req, res) => res.send('Hello World!'))

app.post('/analyze', async (req, res) => {
const documents = req.body

const key_var = 'TEXT_ANALYTICS_SUBSCRIPTION_KEY';
if (!process.env[key_var]) {
throw new Error('please set/export the following environment variable: ' + key_var);
}
const subscription_key = process.env[key_var];

const endpoint_var = 'TEXT_ANALYTICS_ENDPOINT';
if (!process.env[endpoint_var]) {
throw new Error('please set/export the following environment variable: ' + endpoint_var);
}
const endpoint = process.env[endpoint_var];

const creds = new CognitiveServicesCredentials.ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': subscription_key } });
const client = new TextAnalyticsAPIClient.TextAnalyticsClient(creds, endpoint);

const inputLanguage = { documents };


// Sentiment Analysis
let sentimentResult = await client.sentiment({ multiLanguageBatchInput: inputLanguage })
let keyPhrasesResult = await client.keyPhrases({ multiLanguageBatchInput: inputLanguage })

let result = {
sentiment: sentimentResult,
keyPhrases: keyPhrasesResult
}

res.json(result)
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))
1 change: 1 addition & 0 deletions expressapp/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6b61e21

Please sign in to comment.