You will be muted until you read the rules. Read them! @Rules_for_Python A group about the Python programming language. Offtopic things go here: @pythonofftopic Resources to learn python: @pythonres Group for Hy: @hylang Selenium: @SeleniumPython
from flask import Flask, request, jsonify
import random
from textblob import TextBlob
app = Flask(name)
# Sample movie dataset with common emotions
movies = [
{"title": "Inside Out", "genre": "Animation", "emotion": "sad", "description": "A journey through emotions."},
{"title": "The Pursuit of Happyness", "genre": "Drama", "emotion": "sad", "description": "A story of hope and perseverance."},
{"title": "Inception", "genre": "Sci-Fi", "emotion": "bored", "description": "A mind-bending thriller."},
{"title": "The Grand Budapest Hotel", "genre": "Comedy", "emotion": "happy", "description": "A quirky and fun adventure."},
{"title": "Up", "genre": "Animation", "emotion": "happy", "description": "A heartwarming story about adventure and love."},
{"title": "A Beautiful Mind", "genre": "Drama", "emotion": "anxious", "description": "A tale of overcoming inner turmoil."},
{"title": "The Avengers", "genre": "Action", "emotion": "stressed", "description": "An action-packed superhero film."},
{"title": "Good Will Hunting", "genre": "Drama", "emotion": "depressed", "description": "A story of finding purpose and connection."},
]
# Map of similar emotions
emotion_map = {
"angry": "stressed",
"frustrated": "stressed",
"overwhelmed": "anxious",
"lonely": "sad",
"melancholic": "sad",
"excited": "happy",
"content": "happy",
"bored": "bored",
"worried": "anxious",
}
# Function to recommend a movie based on emotion
def recommend_movie(emotion):
# Normalize the input emotion
emotion = emotion.lower()
# Check for direct match in the movie dataset
filtered_movies = [movie for movie in movies if movie["emotion"] == emotion]
if filtered_movies:
return random.choice(filtered_movies)
# Check if the emotion can be mapped to a known category
if emotion in emotion_map:
mapped_emotion = emotion_map[emotion]
filtered_movies = [movie for movie in movies if movie["emotion"] == mapped_emotion]
if filtered_movies:
return random.choice(filtered_movies)
# Use sentiment analysis to infer emotion if no match is found
sentiment = TextBlob(emotion).sentiment
if sentiment.polarity > 0.2:
inferred_emotion = "happy"
elif sentiment.polarity < -0.2:
inferred_emotion = "sad"
else:
inferred_emotion = "bored"
# Return a movie based on inferred emotion
filtered_movies = [movie for movie in movies if movie["emotion"] == inferred_emotion]
if filtered_movies:
return random.choice(filtered_movies)
# Fallback: Provide a general movie suggestion if no match is found
return {
"title": "The Shawshank Redemption",
"genre": "Drama",
"emotion": "general",
"description": "A timeless story of hope and resilience."
}
# API endpoint for getting movie recommendation
@app.route("/recommend", methods=["POST"])
def get_recommendation():
data = request.get_json()
emotion = data.get("emotion")
if not emotion:
return jsonify({"error": "Emotion input is required."}), 400
recommendation = recommend_movie(emotion)
return jsonify(recommendation)
# Root endpoint
@app.route("/", methods=["GET"])
def home():
return "Welcome to the Emotion-based Movie Recommendation API!"
if name == "main":
app.run(debug=True)
please help me run this code in python
I am trying to create a music player using python. I want to add a feature in it that if the song ends starts new song and also simulataneously process user arguments parallely so please can any one help me that how to do this
Module Using for play Music : Pygame
I don't work with fastapi, but it appears to me what you want is to return the data of the enum. For which case you can create a model:
class CountryCodesResponse(BaseModel):Читать полностью…
codes: list[str]
I mean, some functions won't return and do some stuff with an object. But generally it's a good practice to copy the elements to not make your execution confusing.
Читать полностью…note: example
some_list = []
def foo(bar):
bar.append("value")
print(bar)
for i in range(10):
foo(some_list)
The default value is evaluated when the function is defined. That happens only once. So instead of getting a new object on each function call, you get a reference to the same object.
That is why mutable default values shouldn't be used at all. Instead, you can simply assign a list to var
in the body of the function. If you do that by including its desired value, you don't have to use the append
method later.
Hello everyone !
Could anyone explain me one feature:
def vv(var = []):
var.append(0)
print(var)
for _ in range(10):
vv()
with every iteration, zeros appending to my var list, but why? We are assign var = [] after every func call, but zeros is not removing.
So, i've expected output:
[0]
[0]
... and so on
But i've got:
(at last iteration)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Eyy guys I really want to lean python because because I want to use it in the development of my school project please share some links for videos suitable for beginners
Читать полностью…Guys, hello to all, I'm just starting to learn Python programming, can you please advise me how to learn it correctly and what video lessons to watch? Thank you
Читать полностью…Yeah i will learn AI i chose python for this but i want to learn web development. I will for 5 years or more and then learn math and AI
Читать полностью…If you want to learn python, I think it will be better if you learn more about data science.
Читать полностью…Advice: Learn a lot, don't try to learn by just wasting videos.
Probably django has more positions available.
Does anyone know the dynamic import of modules?
Any useful links for reference?
I think this is the most logical and correct approach for my task. Do you have any better ideas?
Читать полностью…Hello everyone! I have a question.
There is the following StrEnum for storing country codes.
To get country codes I use the 'pycountry' library.
class CountryCode(StrEnum):
@classmethod
def _initialize(cls):
for country in pycountry.countries:
if not hasattr(cls, country.alpha_2):
setattr(cls, country.alpha_2, country.alpha_2)
CountryCode._initialize()
pydantic.errors.PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.IsInstanceSchema (<enum 'CountryCode'>)
First of all, modifying the object you get passed in a function... in general it's bad.
Читать полностью…Maybe it's because list are staying in the locals when the cycle is on
Читать полностью…Check out @PythonRes, a channel for Python resources - video courses, pdf books, tutorials and other info to help you learn Python.
Читать полностью…https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
Читать полностью…Can anyone recommend what language to learn to be a developer
Читать полностью…Thanks i'm doing like that learn by doing is faster and mor memorable
Читать полностью…Can anyone gives advice for learning python young man (for backend )
And next question is I'm learning fastapi but django which is main framwevork in backend and which is have more vacation's i think it's django but fastapi is improving every day