jadend's picture
Update app.py
6a288ee verified
Raw History Blame Contribute Delete
4.01 kB
# import the relevant packages
import os
import csv
import openai
from openai import OpenAI
import gradio as gr
import huggingface_hub
from huggingface_hub import Repository
from datetime import datetime
client = OpenAI(
api_key=os.environ.get("API_TOKEN"),
)
# recording requests to Hugging Face dataset
# DATASET_REPO_URL = "https://huggingface.co/datasets/jadend/mr_bot_requests"
# DATA_FILENAME = "prompt_input.csv"
# DATA_FILE = os.path.join("data", DATA_FILENAME)
# HF_TOKEN = os.environ.get("HF_TOKEN")
# repo = Repository(
# local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
# )
# function to store the message to the Hugging Face site, if you would like to add more columns or information to track add it here
# def store_message(prompt: str, temperature: str, response: str):
# if prompt:
# with open(DATA_FILE, "a") as csvfile:
# writer = csv.DictWriter(csvfile, fieldnames=["prompt", "temperature", "response", "time"])
# writer.writerow(
# {"prompt": prompt,
# "temperature": temperature,
# "response": response,
# "time": str(datetime.now())}
# )
# commit_url = repo.push_to_hub()
# print(commit_url)
#
# return
# defines who can enter the application with the secrets that are set up if used
user_db = {
os.environ["username"]: os.environ["password"],
}
# the OpenAI function to use the given prompt to the model defined.
def mr_bot(prompt, tokens, temperature):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=tokens,
temperature=temperature,
)
# store_message(prompt=prompt, temperature=temperature, response=response.choices[0].message.content)
return response.choices[0].message.content
# Gradio App interface and login set up
description = "Use the examples below to see how different prompts generate different results. Change the the Temperture to see how the prompts become more rigid and repeatable at lower temperatures and more creative at higher temperatures. Change the token limit to adjust the length of the response you are recieving, which also changes the type of response you will receive."
demo = gr.Interface(fn=mr_bot,
inputs=[gr.Textbox(placeholder="Select an example", interactive=False), gr.Slider(1, 800, value=800), gr.Slider(0.1, 1, value=0.75)],
outputs="text",
title="Intro to AI, Temperature Control",
description=description,
cache_examples=False,
examples = [
# Creative Writing:
["Describe a day in the life of a wizard with a Corgi sidekick living in Philadelphia.", 800, .95],
# Open-ended Question:
["What are some unconventional uses for a paperclip?", 800, .50],
# Sentiment Analysis:
["How has consumer sentiment changed since the pandemic?", 800, .60],
# Invention Ideas:
["Imagine a new gadget that could change how we work from home. Describe it.", 800, .50],
# Coding Scenario:
["Write me a function in python that involves rolling two 20-sided die and adding them by a user generated number.", 800, .40],
# Office Tasking:
["Write me an email to my boss asking to meet about the upcoming conference.", 800, .50],
]
)
if __name__ == "__main__":
demo.launch(
# enable_queue=False,
## if authentication is needed, uncomment below
# auth=lambda u, p: user_db.get(u) == p,
# auth_message="Welcome!"
)