Skip to content

Instantly share code, notes, and snippets.

View SidJain1412's full-sized avatar
👨‍💻

Siddharth Jain SidJain1412

👨‍💻
View GitHub Profile
@SidJain1412
SidJain1412 / random_color.py
Created April 7, 2021 12:52
Python Random Hex
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'
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
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
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
@SidJain1412
SidJain1412 / call_streaming_api.py
Created May 11, 2023 11:20
Calling a Streaming API using requests library in Python
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:
@SidJain1412
SidJain1412 / simple_app.py
Last active May 12, 2023 09:52
Basic FastAPI app
# 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,
)
@SidJain1412
SidJain1412 / fastapi_openai.py
Created May 11, 2023 09:09
Simple example using FastAPI and OpenAI to create an API
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)