Google Geminiを使ってみた!

1. Pythonライブラリのインストール

pip install google-cloud-aiplatform

Successfully installed google-auth-2.25.2 google-cloud-aiplatform-1.38.1
google-cloud-resource-manager-1.11.0 google-cloud-storage-2.14.0
google-resumable-media-2.7.0 grpc-google-iam-v1-0.13.0 shapely-2.0.2

2. サンプルのテスト

Chatプロンプトのテスト

テストプログラム作成

vi gemini_test.py

コード抜粋

 chat = chat_model.start_chat(
        context="My name is Miles. You are an astronomer, knowledgeable about the solar system.",
        examples=[
            InputOutputTextPair(
                input_text="How many moons does Mars have?",
                output_text="The planet Mars has two moons, Phobos and Deimos.",
            ),
        ],
    )
  1. 実行
    Google Cloud上のサーバで実行しました。
python3 gemini_test.py
  1. ちゃんと結果が来た
Response from Model: There are eight planets in the solar system:
Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.

3分でできる!Google 翻訳API

1. APIコンソールを開く

APIコンソールを開いて、有効化されていることを確認

https://console.cloud.google.com/apis/library?hl=ja

2. サーバに資格認証の設定をする

gcloud auth application-default login

3. pip install

$ pip install google-cloud-translate

4. テストコードを作成

def translate_text(target: str, text: str) -> dict:
    """Translates text into the target language.

    Target must be an ISO 639-1 language code.
    See https://g.co/cloud/translate/v2/translate-reference#supported_languages
    """
    from google.cloud import translate_v2 as translate

    translate_client = translate.Client()

    if isinstance(text, bytes):
        text = text.decode("utf-8")

    # Text can also be a sequence of strings, in which case this method
    # will return a sequence of results for each text.
    result = translate_client.translate(text, target_language=target)

    print("Text: {}".format(result["input"]))
    print("Translation: {}".format(result["translatedText"]))
    print("Detected source language: {}".format(result["detectedSourceLanguage"]))

    return result

translate_text("ja","this is test")

5. 翻訳実行!

$ python3 gcloud_translate.py
Text: this is test
Translation: これはテストです
Detected source language: en

yfinance がバグったので直してみた

株価取得プログラムが動かない

原因調査のため、サンプルプログラムを作成して、実行

import mysql.connector
import yfinance as yf
import json

ticker = yf.Ticker("GOOG")

print(ticker.info)
 File "/usr/local/lib/python3.9/dist-packages/yfinance/data.py", line 209, in get_raw_json
    response.raise_for_status()
  File "/usr/local/lib/python3.9/dist-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/GOOG?modules=summaryProfile%2CfinancialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&ssl=true

確かにエラーになる。。ふむふむ、これは調査せねば

GitHubを調べた

https://github.com/ranaroussi/yfinance/issues/1592
HTTPError: 401 Client Error: Unauthorized for url
とある。これだな。
これはサードパーティのライブラリで、
Yahoo側もいろいろ差し替えるから、いたちごっこなんだろう

対応方法

pip をアップデート

$ pip install yfinance -U
Requirement already satisfied: yfinance in /usr/local/lib/python3.9/dist-packages (0.2.18)
Collecting yfinance
  Downloading yfinance-0.2.26-py2.py3-none-any.whl (62 kB)
     |????????????????????????????????| 62 kB 705 kB/s
Requirement already satisfied: html5lib>=1.1 in /usr/local/lib/python3.9/dist-packages (from yfinance) (1.1)
Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.9/dist-packages (from yfinance) (0.0.11)
Requirement already satisfied: frozendict>=2.3.4 in /usr/local/lib/python3.9/dist-packages (from yfinance) (2.3.8)
Requirement already satisfied: requests>=2.31 in /usr/local/lib/python3.9/dist-packages (from yfinance) (2.31.0)
Requirement already satisfied: lxml>=4.9.1 in /usr/local/lib/python3.9/dist-packages (from yfinance) (4.9.2)
Requirement already satisfied: numpy>=1.16.5 in /usr/local/lib/python3.9/dist-packages (from yfinance) (1.24.3)
Requirement already satisfied: appdirs>=1.4.4 in /usr/local/lib/python3.9/dist-packages (from yfinance) (1.4.4)
Requirement already satisfied: beautifulsoup4>=4.11.1 in /usr/local/lib/python3.9/dist-packages (from yfinance) (4.12.2)
Requirement already satisfied: pandas>=1.3.0 in /usr/local/lib/python3.9/dist-packages (from yfinance) (2.0.2)
Requirement already satisfied: pytz>=2022.5 in /usr/local/lib/python3.9/dist-packages (from yfinance) (2023.3)
Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.9/dist-packages (from beautifulsoup4>=4.11.1->yfinance) (2.4.1)
Requirement already satisfied: six>=1.9 in /usr/lib/python3/dist-packages (from html5lib>=1.1->yfinance) (1.16.0)
Requirement already satisfied: webencodings in /usr/local/lib/python3.9/dist-packages (from html5lib>=1.1->yfinance) (0.5.1)
Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.3.0->yfinance) (2.8.2)
Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.3.0->yfinance) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.9/dist-packages (from requests>=2.31->yfinance) (3.1.0)
Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3/dist-packages (from requests>=2.31->yfinance) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests>=2.31->yfinance) (2020.6.20)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/lib/python3/dist-packages (from requests>=2.31->yfinance) (1.26.5)
Installing collected packages: yfinance
  WARNING: The script sample is installed in '/home/hiroshi855/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed yfinance-0.2.26


修正確認

なおった。よかったー

h$ python3 yfinance_test.py
{'address1': '1600 Amphitheatre Parkway', 'city': 'Mountain View', 'state': 'CA', 'zip': '94043', 'country': 'United States', 'phone': '650 253 0000', 'website': 'https://www.abc.xyz', 'industry': 'Internet Content & Information', 'industryDisp': 'Internet Content & Information', 'sector': 'Communication Services', 'longBusinessSummary': 'Alphabet Inc. offers various products and platforms in the United States, Europe, the Middle East, Africa, the Asia-Pacific, Canada, and Latin America. It operates through Google Services, Google Cloud, and Other Bets segments. The Google Services segment provides products and services, including ads, Android, Chrome, hardware, Gmail, Google Drive, Google Maps, Google Photos, Google Play, Search, and YouTube. It is also involved in the sale of apps and in-app purchases and digital content in the Google Play store; and Fitbit wearable devices, Google Nest home products, Pixel phones, and other devices, as well as in the provision of YouTube non-advertising services. The Google Cloud segment offers infrastructure, cybersecurity, data, analytics, AI, and machine learning, and other services; Google Workspace that include cloud-based collaboration tools for enterprises, such as Gmail, Docs, Drive, Calendar, and Meet; and other services for enterprise customers. The Other Bets segment sells health technology and internet services. The company was founded in 1998 and is headquartered in Mountain View, California.', 'fullTimeEmployees': 190711, 'companyOfficers': [{'maxAge': 1, 'name': 'Mr. Sundar  Pichai', 'age': 49, 'title': 'CEO & Director', 'yearBorn': 1973, 'fiscalYear': 2022, 'totalPay': 7947461, 'exercisedValue': 39618220, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Lawrence Edward Page', 'age': 49, 'title': 'Co-Founder & Director', 'yearBorn': 1973, 'fiscalYear': 2022, 'totalPay': 1, 'exercisedValue': 0

pythonでJ-Quants

J-Quants はJPX(日本証券取引所)の提供するAPI

無料プランの場合、3か月遅れの情報までしか取得できない

pypi


import json
import requests
import pandas as pd


# POSTするデータを作る。
email  = "xxxxxxxxxxxxx"
password = "xxxxxxxxxx"
account_data = json.dumps({
        "mailaddress": email,
        "password": password,
    })

auth_user_url = "https://api.jquants.com/v1/token/auth_user"

auth_result = requests.post(auth_user_url, data=account_data)
refresh_token = auth_result.json()["refreshToken"]

auth_refresh_url=f"https://api.jquants.com/v1/token/auth_refresh?refreshtoken={refresh_token}"

refresh_result = requests.post(auth_refresh_url)
id_token = refresh_result.json()["idToken"]

code = "7203" # 4桁のコードでも5桁のコード72030でもよい。
from_ = "2023-03-01"
to_ = "2023-03-01"

daily_quotes_url = f"https://api.jquants.com/v1/prices/daily_quotes?code={code}&from={from_}&to={to_}"
target_url= f"https://api.jquants.com/v1/fins/statements?code={code}"

# idトークンはヘッダーにセットする
headers = {"Authorization": f"Bearer {id_token}"}
result = requests.get(target_url, headers=headers)
print(result.json())

pythonでのYahoo Finance API

yfinanceのインストール

yfinanceはpythonのでYahoo Financeにアクセスするためのライブラリです。個人利用しか出来ないです。

https://pypi.org/project/yfinance/

pipをインストールして、yahoo finance ライブラリをインストールします。

   # apt install pip
   # pip install yfinance

テーブル作成

json型を使います。

    create database finance;
    CREATE USER 'finance'@'localhost' IDENTIFIED BY 'xxxxx';
    GRANT ALL PRIVILEGES ON * . * TO 'finance'@'localhost';
    use finance;
    CREATE TABLE YahooFINANCE (
      t_date date NOT NULL,
      ticker VARCHAR(255) NOT NULL,
      attr JSON,
      CHECK (JSON_VALID(attr)),
      PRIMARY KEY (t_date,ticker)
   );

GOOGLEの情報をデータベースにinsertし、selectするpy

import mysql.connector
import yfinance as yf
import json

ticker = yf.Ticker("GOOG")

conn = mysql.connector.connect(
    user="finance",
    password="xxxxx",
    host="localhost",
    database="finance")
cur = conn.cursor() 

sql = ('INSERT INTO YahooFINANCE (t_date, ticker, attr) VALUES (now(6), %s, %s) ON DUPLICATE KEY UPDATE attr = VALUES (attr);')

data = [
     ('GOOG', json.dumps(ticker.info)) 
]

cur.executemany(sql, data)

conn.commit() 

cur.execute("SELECT t_date,ticker FROM YahooFINANCE", ()) 

for id,ticker in cur: 
    print(f"TICKER: {ticker}, ID: {id}")

conn.commit()