Skip to content

Commit

Permalink
tweak suggestion providing for more sensible results
Browse files Browse the repository at this point in the history
  • Loading branch information
Batleram committed Sep 29, 2024
1 parent 76c3ce1 commit 80cf8f5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 85 deletions.
108 changes: 40 additions & 68 deletions App/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { generateWidgets } from './utils/generateWidgets'
import { CategoryType } from './types/Categories'
import AnalyzingVoice from './components/categories/AnalyzingVoice'
import Initialization from './Initialization'
import { CONFIG } from './config'

function App() {
return (
Expand All @@ -24,17 +25,17 @@ const AppProvider = () => {
const [onInitialLoad, setOnInitialLoad] = useState(true);
const [name, setName] = useState<string | null>(null);
useEffect(() => {
if (!localStorage.getItem('name')) {
setOnInitialLoad(true);
} else {
setOnInitialLoad(false);
}
if (!localStorage.getItem('name')) {
setOnInitialLoad(true);
} else {
setOnInitialLoad(false);
}
}, [name])

console.log(name)
return (
<>
{ onInitialLoad ? <InitializeApp setName={setName} /> : <NonInitializedApp /> }
{onInitialLoad ? <InitializeApp setName={setName} /> : <NonInitializedApp />}
</>
);
}
Expand All @@ -44,13 +45,13 @@ interface InitializeAppProps {
}
const InitializeApp = (props: InitializeAppProps) => {
return (
<>
<div className="inital h-full w-full">
<>
<Initialization setName={props.setName} />
<div className="inital h-full w-full">
<>
<Initialization setName={props.setName} />
</>
</div>
</>
</div>
</>
);
}

Expand All @@ -67,73 +68,44 @@ const NonInitializedApp = () => {
setConfigState(true);
}
}, [configState]);



useEffect(() => {
const newWidgets = generateConfig();
/*
for (const slide of slideData) {
if (slide instanceof EndOfDayWidgetData) {
newWidgets.push(
<Widget>
End of day Widget
</Widget>
);
}
else if (slide instanceof SuggestionWidgetData) {
newWidgets.push(
<Widget>
Suggestion: {slide.suggestion}
</Widget>
);
}
else if (slide instanceof EndWidgetData) {
newWidgets.push(
<Widget>
There are no more wigets :(
</Widget>
);
} else {
console.error("Unknown slide type", slide)
}
}*/

setWidgets(newWidgets);
useEffect(() => {
generateConfig().then(setWidgets).catch(console.error)
}, []);

return (
/// start with the logic that collects the voice recording then transition to the widgets
/// start with the logic that collects the voice recording then transition to the widgets
<>
{configState ? <AnalyzingVoice setConfigState={setConfigState} /> :
<Container>
<VerticalCarousel slides={widget}>
</VerticalCarousel>
</Container>
}
{configState ? <AnalyzingVoice setConfigState={setConfigState} /> :
<Container>
<VerticalCarousel slides={widget}>
</VerticalCarousel>
</Container>
}
</>
);
}

function generateConfig() {
const widgetConfig = {
[CategoryType.EXERCISE]: ['You should go for a Run', 'Yoga', 'Gym'],
[CategoryType.SOCIAL]: ['Call a friend', 'Join a club', 'Attend a meetup'],
[CategoryType.FOOD]: ['Eat a salad', 'Try a new recipe', 'Have a smoothie'],
[CategoryType.WATER]: ['Drink a glass of water', 'Have some herbal tea', 'Stay hydrated']
};

const widgetData = {
config: widgetConfig,
numberOfWidgets: 30
};

const config = generateWidgets(widgetData);

console.log(config);

return (config);
function generateConfig(): Promise<React.ReactNode[]> {
return new Promise((resolve, reject) => {
fetch(`${CONFIG.BACKEND_HOST}/suggest`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
topics: [CategoryType.EXERCISE, CategoryType.SOCIAL, CategoryType.FOOD, CategoryType.WATER],
count: 3
})
})
.then(data => data.json())
.then(suggestions => {
const widgets = generateWidgets({ config: suggestions, numberOfWidgets: 15 })
resolve(widgets);
})
.catch(reject)
})
}

3 changes: 2 additions & 1 deletion App/src/utils/generateWidgets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export const generateWidgets = (props: Widget): React.ReactNode[] => {
widgets.push(<Widget><Water drink={content[0]} /></Widget>);
}
else {
const affirmation = Affirmations[0];
const index = Math.floor(Math.random() * Affirmations.length);
const affirmation = Affirmations[index];
widgets.push(<Widget><Affirmation affirmation={affirmation}></Affirmation></Widget>);
}
}
Expand Down
47 changes: 31 additions & 16 deletions Server/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, request, jsonify
from flask import Flask, Response, request, jsonify
from flask_cors import CORS, cross_origin
from transformers import pipeline
import os
Expand Down Expand Up @@ -81,22 +81,37 @@
]
}

# Expects { topics: string[], count: number}
@app.route("/suggest", methods=["POST"])
def suggest():
body = request.get_json()

@app.route("/analyze", methods=["POST"])
def analyze():
input_text = request.json.get("text")
category = classify_sentence(input_text)
goal_status = classify_goal_status(input_text)
goal_suggestions = []
if goal_status == 0:
for i in range(4):
selection = random.choice(suggestions[category])
goal_suggestions.append(selection)
return jsonify({"goal_status": goal_status, "suggestions": goal_suggestions})

def classify_sentence(sentence):
result = sentence_classifier(sentence, goal_categories)
return result['labels'][0]
if not body:
return Response("Invalid argument", status=400)

try:
topics = body["topics"]
count = int(body["count"])
except:
return Response("Invalid argument", status=400)

# Whitelist topics
topics = filter(lambda x: x in goal_categories, topics)

if not topics or not count:
return Response("Invalid argument", status=400)

output = {}
for topic in topics:
for i in range(0, min(count, len(suggestions[topic]))):
if not topic in output:
output[topic] = []
suggestion = random.choice(suggestions[topic])
while suggestion in output[topic]:
suggestion = random.choice(suggestions[topic])
output[topic].append(suggestion)

return jsonify(output)

def classify_goal_status(sentence):
result = goal_status_pipe(sentence)
Expand Down

0 comments on commit 80cf8f5

Please sign in to comment.