Commit e77f53d8 authored by vuong's avatar vuong

add gemini

parent 6f04226c
from fastapi import FastAPI, Request, Form
from fastapi.templating import Jinja2Templates
import os
import google.generativeai as genai
from datetime import datetime
os.environ['GOOGLE_API_KEY'] = "AIzaSyCKushgizShplWONRJ3_hdPDPIYnRVF5Mw"
genai.configure(api_key = os.environ['GOOGLE_API_KEY'])
# import PIL
# image = PIL.Image.open('assets/sample_image.jpg')
# vision_model = genai.GenerativeModel('gemini-pro-vision')
# response = vision_model.generate_content(["Explain the picture?",image])
# print(response.text)
current_date = datetime.now().strftime("%d/%m/%Y")
role_prompt = f"""
You are a data analysis chatbot.
You will analyze the data sent in JSON format and provide the result, which is the quantity needed to restock for each product for the next month (Only for the next month) and provide a brief explanation along with the product data (reason).
The meanings of the data are as follows:
1.order_qty: the quantity needed to restock.
2.qty_expected_avrage_Q1: the expected quantity to be sold in quarter 1.
3.qty_sold_avrage_Q1: the average quantity sold in quarter 1.
4.days_in_stock_Q1: the number of days the product remained in stock in quarter 1.
5.qty_expected_avrage_Q2: the expected quantity to be sold in quarter 2.
6.qty_sold_avrage_Q2: the average quantity sold in quarter 2.
7.days_in_stock_Q2: the number of days the product remained in stock in quarter 2.
8.qty_expected_avrage_Q3: the expected quantity to be sold in quarter 3.
9.qty_sold_avrage_Q3: the average quantity sold in quarter 3.
10.days_in_stock_Q3: the number of days the product remained in stock in quarter 3.
11.qty_expected_avrage_Q4: the expected quantity to be sold in quarter 4.
12.qty_sold_avrage_Q4: the average quantity sold in quarter 4.
13.days_in_stock_Q4: the number of days the product remained in stock in quarter 4.
14.qty_last_halloween: the quantity sold during last year's Halloween.
15.qty_last_xmas: the quantity sold during last year's Christmas.
16.qtysold_30: the quantity sold in the last 30 days.
17.qtysold_60: the quantity sold in the last 60 days.
18.qtysold_90: the quantity sold in the last 90 days.
19.qtysold_180: the quantity sold in the last 180 days.
20.qtysold_720: the quantity sold in the last 2 years.
21.days_in_stock_12: the total number of days the product remained in stock in the last 12 months.
22.days_in_stock_24: the total number of days the product remained in stock in the last 24 months.
23.qty: the quantity of the product in stock.
24.created_at: the date the product was created.
25.b_fill_up_date: the date of the most recent restock.
26.qty_ordered: the total quantity sold.
27.qty_per_month: the average quantity sold per month.
28.last_time_sold: the most recent date the product was sold.
29.vendor_last_fillup_qty: the quantity restocked most recently.
30.qtysold_2024: the quantity sold in the year 2024.
31.qtysold_2023: the quantity sold in the year 2023.
32.qtysold_2022: the quantity sold in the year 2022.
33.qtysold_2021: the quantity sold in the year 2021.
34.qtysold_2020: the quantity sold in the year 2020.
Currently, it's {current_date}.
These products are candies, pastries, and children's toys sold in Norway. Please pay attention to holidays and major festivals when these products are heavily consumed for analysis.
The output is in JSON format with the following data: product_id, sku, order_qty, and reason.
"""
# system = [{"role": "system", "content": role_prompt}]
# user = [{"role": "user", "content": "Who are you?"}]
# messages = system + user
messages = role_prompt + 'Data: [{"product_id":126543,"sku":141589,"order_qty":null,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":5,"days_in_stock_Q1":46,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":0,"days_in_stock_Q2":0,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":0,"days_in_stock_Q3":0,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":0,"days_in_stock_Q4":0,"qty_last_halloween":0,"qty_last_xmas":0,"qtysold_30":4,"qtysold_60":4,"qtysold_90":4,"qtysold_180":4,"qtysold_720":4,"days_in_stock_12":46,"days_in_stock_24":46,"qty":2,"created_at":"2011-09-23","b_fill_up_date":"2016-12-14","qty_ordered":66,"qty_per_month":0.4,"last_time_sold":"2024-02-20","vendor_last_fillup_qty":null,"qtysold_2024":4,"qtysold_2023":0,"qtysold_2022":0,"qtysold_2021":0,"qtysold_2020":0},{"product_id":217688,"sku":191498,"order_qty":null,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":1,"days_in_stock_Q1":90,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":2,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":6,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":9,"days_in_stock_Q4":92,"qty_last_halloween":1,"qty_last_xmas":8,"qtysold_30":0,"qtysold_60":0,"qtysold_90":5,"qtysold_180":16,"qtysold_720":31,"days_in_stock_12":365,"days_in_stock_24":681,"qty":1,"created_at":"2022-01-10","b_fill_up_date":"2023-10-30","qty_ordered":34,"qty_per_month":1.3,"last_time_sold":"2023-12-26","vendor_last_fillup_qty":null,"qtysold_2024":0,"qtysold_2023":19,"qtysold_2022":15,"qtysold_2021":null,"qtysold_2020":null},{"product_id":221220,"sku":122199,"order_qty":null,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":2,"days_in_stock_Q1":88,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":1,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":1,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":4,"days_in_stock_Q4":79,"qty_last_halloween":2,"qty_last_xmas":2,"qtysold_30":1,"qtysold_60":2,"qtysold_90":3,"qtysold_180":8,"qtysold_720":12,"days_in_stock_12":350,"days_in_stock_24":465,"qty":1,"created_at":"2022-09-14","b_fill_up_date":"2024-01-03","qty_ordered":12,"qty_per_month":0.7,"last_time_sold":"2024-02-29","vendor_last_fillup_qty":3,"qtysold_2024":2,"qtysold_2023":6,"qtysold_2022":4,"qtysold_2021":null,"qtysold_2020":null},{"product_id":217643,"sku":42511,"order_qty":null,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":3,"days_in_stock_Q1":90,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":1,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":0,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":4,"days_in_stock_Q4":92,"qty_last_halloween":1,"qty_last_xmas":7,"qtysold_30":3,"qtysold_60":3,"qtysold_90":5,"qtysold_180":8,"qtysold_720":22,"days_in_stock_12":365,"days_in_stock_24":730,"qty":2,"created_at":"2022-01-07","b_fill_up_date":"1970-01-01","qty_ordered":22,"qty_per_month":0.8,"last_time_sold":"2024-02-26","vendor_last_fillup_qty":null,"qtysold_2024":3,"qtysold_2023":8,"qtysold_2022":11,"qtysold_2021":null,"qtysold_2020":null},{"product_id":220858,"sku":346260,"order_qty":null,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":2,"days_in_stock_Q1":90,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":0,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":1,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":2,"days_in_stock_Q4":92,"qty_last_halloween":0,"qty_last_xmas":4,"qtysold_30":0,"qtysold_60":2,"qtysold_90":3,"qtysold_180":5,"qtysold_720":9,"days_in_stock_12":365,"days_in_stock_24":533,"qty":1,"created_at":"2022-08-31","b_fill_up_date":"2022-12-16","qty_ordered":9,"qty_per_month":0.5,"last_time_sold":"2024-01-25","vendor_last_fillup_qty":null,"qtysold_2024":2,"qtysold_2023":3,"qtysold_2022":4,"qtysold_2021":null,"qtysold_2020":null},{"product_id":217949,"sku":173746,"order_qty":null,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":8,"days_in_stock_Q1":76,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":6,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":10,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":0,"days_in_stock_Q4":92,"qty_last_halloween":2,"qty_last_xmas":1,"qtysold_30":4,"qtysold_60":6,"qtysold_90":6,"qtysold_180":24,"qtysold_720":34,"days_in_stock_12":351,"days_in_stock_24":690,"qty":2,"created_at":"2022-01-20","b_fill_up_date":"2023-07-13","qty_ordered":40,"qty_per_month":1.6,"last_time_sold":"2024-02-21","vendor_last_fillup_qty":null,"qtysold_2024":6,"qtysold_2023":19,"qtysold_2022":15,"qtysold_2021":null,"qtysold_2020":null},{"product_id":218564,"sku":42613,"order_qty":null,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":4,"days_in_stock_Q1":90,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":3,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":2,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":1,"days_in_stock_Q4":80,"qty_last_halloween":1,"qty_last_xmas":5,"qtysold_30":3,"qtysold_60":3,"qtysold_90":4,"qtysold_180":10,"qtysold_720":22,"days_in_stock_12":353,"days_in_stock_24":700,"qty":2,"created_at":"2022-02-28","b_fill_up_date":"2023-12-05","qty_ordered":22,"qty_per_month":0.9,"last_time_sold":"2024-02-17","vendor_last_fillup_qty":null,"qtysold_2024":4,"qtysold_2023":6,"qtysold_2022":12,"qtysold_2021":null,"qtysold_2020":null},{"product_id":211858,"sku":131563,"order_qty":null,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":15,"days_in_stock_Q1":85,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":21,"days_in_stock_Q2":82,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":18,"days_in_stock_Q3":73,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":13,"days_in_stock_Q4":92,"qty_last_halloween":7,"qty_last_xmas":2,"qtysold_30":4,"qtysold_60":12,"qtysold_90":16,"qtysold_180":67,"qtysold_720":106,"days_in_stock_12":332,"days_in_stock_24":659,"qty":4,"created_at":"2019-12-03","b_fill_up_date":"2024-01-25","qty_ordered":150,"qty_per_month":2.9,"last_time_sold":"2024-02-19","vendor_last_fillup_qty":null,"qtysold_2024":12,"qtysold_2023":62,"qtysold_2022":39,"qtysold_2021":23,"qtysold_2020":14},{"product_id":226240,"sku":603428,"order_qty":null,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":0,"days_in_stock_Q1":49,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":0,"days_in_stock_Q2":0,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":0,"days_in_stock_Q3":0,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":0,"days_in_stock_Q4":0,"qty_last_halloween":null,"qty_last_xmas":null,"qtysold_30":2,"qtysold_60":2,"qtysold_90":2,"qtysold_180":2,"qtysold_720":2,"days_in_stock_12":49,"days_in_stock_24":49,"qty":1,"created_at":"2024-01-12","b_fill_up_date":"1970-01-01","qty_ordered":2,"qty_per_month":1.3,"last_time_sold":"2024-03-01","vendor_last_fillup_qty":null,"qtysold_2024":2,"qtysold_2023":null,"qtysold_2022":null,"qtysold_2021":null,"qtysold_2020":null},{"product_id":217958,"sku":42607,"order_qty":null,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":18,"days_in_stock_Q1":90,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":19,"days_in_stock_Q2":90,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":24,"days_in_stock_Q3":83,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":32,"days_in_stock_Q4":92,"qty_last_halloween":16,"qty_last_xmas":20,"qtysold_30":7,"qtysold_60":14,"qtysold_90":26,"qtysold_180":89,"qtysold_720":169,"days_in_stock_12":355,"days_in_stock_24":621,"qty":4,"created_at":"2022-01-20","b_fill_up_date":"2023-08-09","qty_ordered":172,"qty_per_month":6.7,"last_time_sold":"2024-02-28","vendor_last_fillup_qty":null,"qtysold_2024":14,"qtysold_2023":90,"qtysold_2022":68,"qtysold_2021":null,"qtysold_2020":null}]'
# messages = "Who are you?"
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content(messages)
def save_to_text_file(text, filename):
with open(filename, "w") as file:
file.write(text)
# save response in file text
save_to_text_file(response.text, "gemini-response.txt")
# print(response.text)
# chat = model.start_chat(history=[])
# response = chat.send_message("Which is one of the best place to visit in India during summer?")
# print(response.text)
# response = chat.send_message("Tell me more about that place in 50 words")
# print(response.text)
# print(chat.history)
# response = model.generate_content("what are top 5 frequently used emojis?",
# generation_config = genai.types.GenerationConfig(
# candidate_count = 1,
# stop_sequences = ['.'],
# max_output_tokens = 40,
# top_p = 0.6,
# top_k = 5,
# temperature = 0.8)
# )
# print(response.text)
# app = FastAPI()
# templates = Jinja2Templates(directory="templates") # Specify your template directory
# @app.get("/")
# async def root(request: Request):
# return templates.TemplateResponse("index.html", {"request": request}) # Assuming your template is named "index.html"
# @app.post("/")
# async def handle_input(request: Request, user_input: str = Form(...)):
# response_data = model.generate_content(user_input)
# return templates.TemplateResponse("index.html", {"request": request, "response_data": response_data.text})
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment