File size: 1,532 Bytes
b3c6f39 a66feca b3c6f39 581d6d0 b3c6f39 581d6d0 b3c6f39 a66feca 581d6d0 a66feca 581d6d0 27e1e75 581d6d0 a66feca 27e1e75 20514e9 19d5716 20514e9 19d5716 7f1bee4 a66feca 19d5716 a66feca 19d5716 |
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import os
import pandas as pd
import google.generativeai as genai
import gradio as gr
from google.api_core import retry
my_api_key = os.environ.get("GOOGLE-API-KEY")
genai.configure(api_key=my_api_key)
MODEL_NAME = 'gemini-1.5-flash-latest'
retry_policy = {"retry": retry.Retry(predicate=retry.if_transient_error, initial=10, multiplier= 1.5, timeout=300)}
model = genai.GenerativeModel(
MODEL_NAME,
generation_config= genai.GenerationConfig(
temperature= 1.0,
top_p= 1,
max_output_tokens=1000,
)
)
data_path = os.path.join("docs", "Nigerian_Foods.csv")
json_path = os.path.join("docs", "food_data.json")
food_data = pd.read_csv(data_path)
json_data = food_data.to_json(orient="records", lines=False, indent=4)
with open(json_path, "w") as json_file:
json_file.write(json_data)
few_shot_prompt = f"""
You are an interactive recipe assistant. Use the following dataset to recommend recipes:
{json_data}
Instructions:
1. Provide recipes based on the user's query.
2. If the requested recipe is unavailable, suggest the most similar one.
3. Maintain context across multiple messages.
"""
history = []
def recipe_chatbot(messages: str, history: list[str]):
ask = {
"current message" : messages,
"previous message": history[::-1]
}
history.append(messages)
response = model.generate_content([few_shot_prompt,ask], request_options=retry_policy)
return response.text
bot = gr.ChatInterface(
fn=recipe_chatbot,
type="messages"
)
bot.launch()
|