Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sklavit committed Jul 9, 2024
1 parent 9943718 commit 30fbed9
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 121 deletions.
2 changes: 1 addition & 1 deletion apps/streamlit_ds_chat/chat_with_vertex_ai.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import streamlit as st

import vertexai
from vertexai.generative_models import GenerativeModel, Part
from vertexai.generative_models import GenerativeModel
import vertexai.preview.generative_models as generative_models
from google.oauth2.service_account import Credentials

Expand Down
6 changes: 3 additions & 3 deletions apps/streamlit_ds_chat/cloud_coiled_io/executor_1.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import coiled
import importlib

# from test_func import estimate_pi_simple
import test_func

cluster = coiled.Cluster(n_workers=2) # Scale out to 100 machines
client = cluster.get_client()

# from test_func import estimate_pi_simple
import test_func

for i in range(10):
input("go?")
Expand Down
3 changes: 2 additions & 1 deletion apps/streamlit_ds_chat/cloud_coiled_io/serverless_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import coiled, random
import coiled
import random


@coiled.function()
Expand Down
2 changes: 1 addition & 1 deletion apps/streamlit_ds_chat/ds_chat_streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
# stream=False,
# )
# response = api_response.candidates[0].content.parts[0]._raw_part.text
response = f"AI was questioned. Test answer `here`. :) "
response = "AI was questioned. Test answer `here`. :) "
st.markdown(response)

print(("response:", response))
Expand Down
35 changes: 0 additions & 35 deletions apps/streamlit_ds_chat/experiments_standalone/cohere.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
Cannot find a quota project to add to ADC. You might receive a "quota exceeded" or "API not enabled" error. Run $ gcloud auth application-default set-quota-project to add a quota project.
"""

import base64
import vertexai
from vertexai.generative_models import GenerativeModel, Part
from vertexai.generative_models import GenerativeModel
import vertexai.preview.generative_models as generative_models

# import streamlit as st
Expand Down
122 changes: 45 additions & 77 deletions apps/streamlit_ds_chat/experiments_standalone/hf_chat_ms.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,57 @@
import json
from pprint import pprint

import requests
import streamlit as st
from transformers import pipeline

API_TOKEN = st.secrets["HF_API_KEY"]

import requests

headers = {"Authorization": f"Bearer {API_TOKEN}"}
API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-large"
# def query(payload):
# response = requests.post(API_URL, headers=headers, json=payload)
# print('response', response.status_code)
# return response.json()
#
# data = query(
# {
# "inputs": {
# "past_user_inputs": ["Which movie is the best ?"],
# "generated_responses": ["It's Die Hard for sure."],
# "text": "Can you explain why ?",
# },
# }
# )
# # Response
# # This is annoying
# # data.pop("warnings")
# pprint((
# data,
# {
# "generated_text": "It's the best movie ever.",
# "conversation": {
# "past_user_inputs": [
# "Which movie is the best ?",
# "Can you explain why ?",
# ],
# "generated_responses": [
# "It's Die Hard for sure.",
# "It's the best movie ever.",
# ],
# },
# # "warnings": ["Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation."],
# },
# ))

# from transformers import pipeline
#
# messages = [
# {"role": "user", "content": "Who are you?"},
# ]
# pipe = pipeline("text-generation", model="microsoft/DialoGPT-large")
# pipe(messages)

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch


tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")

chat_history_ids = None

# Let's chat for 5 lines
for step in range(5):
# encode the new user input, add the eos_token and return a tensor in Pytorch
new_user_input_ids = tokenizer.encode(
input(">> User:") + tokenizer.eos_token, return_tensors="pt"
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
print("response", response.status_code)
return response.json()


data = query(
{
"inputs": {
"past_user_inputs": ["Which movie is the best ?"],
"generated_responses": ["It's Die Hard for sure."],
"text": "Can you explain why ?",
},
}
)
# Response
# This is annoying
# data.pop("warnings")
pprint(
(
data,
{
"generated_text": "It's the best movie ever.",
"conversation": {
"past_user_inputs": [
"Which movie is the best ?",
"Can you explain why ?",
],
"generated_responses": [
"It's Die Hard for sure.",
"It's the best movie ever.",
],
},
# "warnings": ["Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation."],
},
)
)

# append the new user input tokens to the chat history
bot_input_ids = (
torch.cat([chat_history_ids, new_user_input_ids], dim=-1)
if step > 0
else new_user_input_ids
)

# generated a response while limiting the total chat history to 1000 tokens,
chat_history_ids = model.generate(
bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id
)

# pretty print last ouput tokens from bot
print(
"DialoGPT: {}".format(
tokenizer.decode(
chat_history_ids[:, bot_input_ids.shape[-1] :][0],
skip_special_tokens=True,
)
)
)
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe = pipeline("text-generation", model="microsoft/DialoGPT-large")
pipe(messages)
45 changes: 45 additions & 0 deletions apps/streamlit_ds_chat/experiments_standalone/hf_chat_ms_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch


API_TOKEN = st.secrets["HF_API_KEY"]


headers = {"Authorization": f"Bearer {API_TOKEN}"}
API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-large"


tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")

chat_history_ids = None

# Let's chat for 5 lines
for step in range(5):
# encode the new user input, add the eos_token and return a tensor in Pytorch
new_user_input_ids = tokenizer.encode(
input(">> User:") + tokenizer.eos_token, return_tensors="pt"
)

# append the new user input tokens to the chat history
bot_input_ids = (
torch.cat([chat_history_ids, new_user_input_ids], dim=-1)
if step > 0
else new_user_input_ids
)

# generated a response while limiting the total chat history to 1000 tokens,
chat_history_ids = model.generate(
bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id
)

# pretty print last ouput tokens from bot
print(
"DialoGPT: {}".format(
tokenizer.decode(
chat_history_ids[:, bot_input_ids.shape[-1] :][0],
skip_special_tokens=True,
)
)
)
2 changes: 1 addition & 1 deletion apps/streamlit_ds_chat/vertex_ai_codegen_streamlit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import streamlit as st

import vertexai
from vertexai.generative_models import GenerativeModel, Part
from vertexai.generative_models import GenerativeModel
import vertexai.preview.generative_models as generative_models
from google.oauth2.service_account import Credentials

Expand Down

0 comments on commit 30fbed9

Please sign in to comment.