File size: 1,247 Bytes
04623a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import gensim
from gensim.models import Word2Vec
import gradio as gr

# Prepare and clean your data
# recipes is a list of lists of ingredients. Each list represents a recipe.
# For simplicity, let's assume you have already cleaned your data and it's stored in a variable called recipes.

# Train Word2Vec Model
model = Word2Vec.load("word2vecsg2.model")

def recommend_ingredients(ingredients):
    similar_ingredients = []
    for ingredient in ingredients:
        if ingredient in model.wv.key_to_index:
            top_similar = model.wv.most_similar(ingredient, topn=5)
            for ing, similarity in top_similar:
                similar_ingredients.append((ing, round(similarity, 2)))
    return similar_ingredients

# Create Gradio Interface
def gradio_interface(ingredients):
    ingredients = ingredients.split(',')
    recommendations = recommend_ingredients(ingredients)
    formatted_recommendations = [f'{ing} ({similarity})' for ing, similarity in recommendations]
    return formatted_recommendations

iface = gr.Interface(fn=gradio_interface, 
                     inputs="text", 
                     outputs="text",
                     examples=[["onion,garlic,tomato"], ["beef,chicken"], ["milk,cheese"]])

iface.launch()