Spaces:
Sleeping
Sleeping
Aziz Alto
commited on
Commit
·
d56ba03
1
Parent(s):
c0684b2
Create gpt.py
Browse files
gpt.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from functools import lru_cache
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import anthropic
|
| 6 |
+
import openai
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# st.set_page_config(layout="wide")
|
| 10 |
+
|
| 11 |
+
#
|
| 12 |
+
os.environ[
|
| 13 |
+
"ANTHROPIC_API_KEY"
|
| 14 |
+
] = "sk-ant-hoJDA77YMGZ5Rmxt9IYgzkavTj-VdnwXnJINAgUJCgUHtF9uIFLYaLoCFKGbA7Bupd606KVNaDmbYfTANCPygw"
|
| 15 |
+
|
| 16 |
+
# hackathon-nyc-5
|
| 17 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class AnthropicSerivce:
|
| 21 |
+
def __init__(self):
|
| 22 |
+
self.client = self.anthropic_client()
|
| 23 |
+
|
| 24 |
+
@staticmethod
|
| 25 |
+
@lru_cache
|
| 26 |
+
def anthropic_client():
|
| 27 |
+
return anthropic.Client(os.environ["ANTHROPIC_API_KEY"])
|
| 28 |
+
|
| 29 |
+
def prompt(self, prompt, max_tokens_to_sample: int = 2000):
|
| 30 |
+
response = self.client.completion_stream(
|
| 31 |
+
prompt=f"{anthropic.HUMAN_PROMPT} {prompt}{anthropic.AI_PROMPT}",
|
| 32 |
+
stop_sequences=[anthropic.HUMAN_PROMPT],
|
| 33 |
+
max_tokens_to_sample=max_tokens_to_sample,
|
| 34 |
+
model="claude-v1",
|
| 35 |
+
stream=True,
|
| 36 |
+
)
|
| 37 |
+
return response
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class OpenAIService:
|
| 41 |
+
def __init__(self):
|
| 42 |
+
openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 43 |
+
|
| 44 |
+
def list_models(self):
|
| 45 |
+
return openai.Model.list()
|
| 46 |
+
|
| 47 |
+
def prompt(self, prompt):
|
| 48 |
+
# return openai.Completion.create(model="ada", prompt=prompt)
|
| 49 |
+
return openai.ChatCompletion.create(
|
| 50 |
+
model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}]
|
| 51 |
+
# model="gpt-4", messages=[{"role": "user", "content": prompt}]
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def app():
|
| 56 |
+
prompt = st.text_area("Prompt", value="How many toes do dogs have?", height=100)
|
| 57 |
+
tabs = st.tabs(["Anthropic", "OpenAI"])
|
| 58 |
+
if prompt:
|
| 59 |
+
# -- Antrhopic
|
| 60 |
+
with tabs[0]:
|
| 61 |
+
anthropic_service = AnthropicSerivce()
|
| 62 |
+
response = anthropic_service.prompt(prompt=prompt)
|
| 63 |
+
# for data in response:
|
| 64 |
+
# col = st.columns(2)
|
| 65 |
+
# with col[0]:
|
| 66 |
+
# st.markdown("---")
|
| 67 |
+
# st.write(data)
|
| 68 |
+
# with col[1]:
|
| 69 |
+
# st.markdown("---")
|
| 70 |
+
# st.markdown(data["completion"])
|
| 71 |
+
# -- pick the last response
|
| 72 |
+
|
| 73 |
+
col = st.columns(2)
|
| 74 |
+
answer = list(response)[-1]
|
| 75 |
+
with col[0]:
|
| 76 |
+
st.write(answer)
|
| 77 |
+
with col[1]:
|
| 78 |
+
st.code(answer["completion"])
|
| 79 |
+
|
| 80 |
+
# -- OpenAI
|
| 81 |
+
with tabs[1]:
|
| 82 |
+
openai_service = OpenAIService()
|
| 83 |
+
response = openai_service.prompt(prompt=prompt)
|
| 84 |
+
|
| 85 |
+
col = st.columns(2)
|
| 86 |
+
# st.write(openai_service.list_models())
|
| 87 |
+
with col[0]:
|
| 88 |
+
st.write(response)
|
| 89 |
+
st.markdown("---")
|
| 90 |
+
with col[1]:
|
| 91 |
+
answer = response.choices[0]["message"]["content"]
|
| 92 |
+
st.code(answer, language="python")
|
| 93 |
+
st.markdown("---")
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
|
| 98 |
+
app()
|