This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
url = "http://127.0.0.1:8000/campaign/?prompt=Pepsi" | |
response = requests.get( | |
url, | |
stream=True, | |
headers={"accept": "application/json"}, | |
) | |
for chunk in response.iter_content(chunk_size=1024): | |
if chunk: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI, Query | |
import openai | |
import os | |
import sys | |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "") | |
if not len(OPENAI_API_KEY): | |
print("Please set OPENAI_API_KEY environment variable. Exiting.") | |
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run using `uvicorn simple_app:app --reload` | |
# Visit 127.0.0.1:8000/docs for documentation and testing the API | |
from fastapi import FastAPI, Query | |
app = FastAPI( | |
title="FastAPI Example", | |
description="### Hello world", | |
version=1.0, | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
# How many times each ad was clicked | |
ad_rewards = [0] * bandits | |
# How many times each ad was selected | |
ad_selection = [0] * bandits | |
# N: Number of users | |
N = df.shape[0] | |
# bandits: Number of ads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate_upper_bound(wins, num_selections, n): | |
average_reward = wins / num_selections | |
delta_i = math.sqrt(3/2 * math.log(n + 1) / num_selections) | |
upper_bound = average_reward + delta_i | |
return upper_bound | |
import math | |
import random |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy.stats import beta | |
import random | |
ads_selected = [] | |
# d = number of ads | |
number_of_wins = [0] * d | |
number_of_losses = [0] * d | |
total_reward = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def random_color(): | |
ltemp = np.random.randint(0, 255, 3) | |
return "#%02x%02x%02x" % tuple(ltemp) | |
# col = random_color() | |
# print(col) | |
# '#eb4de9' |