analitics

Pages

Sunday, December 11, 2022

Python 3.10.2 : Quickstart OpenAI example.

Since it's Christmas time, here's a short introduction to OpenAI and python. An example is from the official page.
OpenAI has trained cutting-edge language models that are very good at understanding and generating text. Our API provides access to these models and can be used to solve virtually any task that involves processing language. In this quickstart tutorial, you’ll build a simple sample application. Along the way, you’ll learn key concepts and techniques that are fundamental to using the API for any task, including: Content generation Summarization Classification, categorization, and sentiment analysis Data extraction Translation Many more!
Because I run this example in Windows 10 operating system, you need to step over some commands from the official webpage. For example, this is a Linux command:
. venv/bin/activate
Create a token into an OpenAI account to use it, then use these commands:
git clone https://github.com/openai/openai-quickstart-python.git
cd openai-quickstart-python
pip install Flask
pip install -r requirements.txt --user
Change the app.py source code with your token, like this:
openai.api_key = "your_token"
Run the application with this command:
python -m flask run
The result in the command prompt area is this:
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 102-938-829
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [11/Dec/2022 16:13:06] "POST / HTTP/1.1" 302 -
127.0.0.1 - - [11/Dec/2022 16:13:06] "GET /?result=+The+Flash%2C+Speedy%2C+Sonic+the+Hedgehog HTTP/1.1" 200 -
127.0.0.1 - - [11/Dec/2022 16:13:06] "GET /static/main.css HTTP/1.1" 304 -
127.0.0.1 - - [11/Dec/2022 16:13:06] "GET /static/dog.png HTTP/1.1" 304 -
127.0.0.1 - - [11/Dec/2022 16:13:06] "GET /static/dog.png HTTP/1.1" 304 -
127.0.0.1 - - [11/Dec/2022 16:13:11] "POST / HTTP/1.1" 302 -
127.0.0.1 - - [11/Dec/2022 16:13:11] "GET /?result=+Speedy%2C+The+Flash%2C+Sonic HTTP/1.1" 200 -
127.0.0.1 - - [11/Dec/2022 16:13:12] "GET /static/main.css HTTP/1.1" 304 -
127.0.0.1 - - [11/Dec/2022 16:13:12] "GET /static/dog.png HTTP/1.1" 304 -
This is the result of the running source code:

Tuesday, November 8, 2022

News : Snowpark for Python.

Snowpark for Python, now generally available, empowers the growing Python community of data scientists, data engineers, and developers to build secure and scalable data pipelines and machine learning (ML) workflows directly within Snowflake—taking advantage of Snowflake’s performance, elasticity, and security benefits, which are critical for production workloads., read more on this news on the official blog.
The official website come with this feature: Start your 30-day free Snowflake trial which includes $400 worth of free usage.

Monday, November 7, 2022

Python 3.7.13 : My colab tutorials - part 027.

Today, I update my GitHub repo with python source code tested on colab google.
You can see this notebook named catafest_032.ipynb on my GitHub repo.

Saturday, October 29, 2022

News : PyTorch 1.13 new release.

We are excited to announce the release of PyTorch® 1.13 (release note)! This includes Stable versions of BetterTransformer. We deprecated CUDA 10.2 and 11.3 and completed migration of CUDA 11.6 and 11.7. Beta includes improved support for Apple M1 chips and functorch, a library that offers composable vmap (vectorization) and autodiff transforms, being included in-tree with the PyTorch release. This release is composed of over 3,749 commits and 467 contributors since 1.12.1. We want to sincerely thank our dedicated community for your contributions.
You can find more on the official website.

Friday, October 7, 2022

Python 3.10.7 : Rembg for remove background.

Rembg is a tool to remove images background and the project can be found on the GitHub webpage.
Create a python file named remove_background.py.
Install using the pip tool.
pip install rembg
Collecting rembg
  Downloading rembg-2.0.25-py3-none-any.whl (12 kB)
Add this source code and the input001.png image for procesing in the same folder with the python script.
from rembg import remove 
from PIL import Image 
input_path = 'input001.png'
output_path = 'output001.png'
input = Image.open(input_path)
output = remove(input)
output.save(output_path)
Run the python script and if you see this error:
python remove_background.py
Access denied with the following error:

        Too many users have viewed or downloaded this file recently. Please
        try accessing the file again later. If the file you are trying to
        access is particularly large or is shared with many people, it may
        take up to 24 hours to be able to view or download the file. If you
        still can't access a file after 24 hours, contact your domain
        administrator.

You may still be able to access the file from the browser:

         https://drive.google.com/uc?id=1tCU5MM1LhRgGou5OpmpjBQbSrYIUoYab
         ...
... then copy the u2net.onnx file into this path:
C:\Users\your_user\.u2net\
After I copy the file I run again the python script and this is the result.

Sunday, September 11, 2022

Python : Blockchain Programming - part 001.

This is the first tutorial in the blockchain programming series using the python programming language.
To program a blockchain, we must consider two elements: in addition to the blockchain address, it can have a programmable area and the second essential element, the interaction of the blockchain with external web areas can be programmed.
In this tutorial I will use the python web3 package, an etherium address and an online web utility called infura.io.
In the infura.io account, create a web3 project and in the dashboard - manage key you will have to add the ethereum address and use the url created to mainnet.infura.io.
I created a project in python in Fedora 37 using the conda utility and installed the web3 package.
Here is the python source code I used
from web3 import Web3
node_provider = "https://mainnet.infura.io/v3/1f2fb5d1e1be4c11acdbbb07a2e06a1c"

web3_connection = Web3(Web3.HTTPProvider(node_provider))


def is_connected():
    print(web3_connection.isConnected())

def latest_block():
    print(web3_connection.eth.block_number)

def balanceETH(ETH_address):
    balance = web3_connection.eth.get_balance(ETH_address)
    balance_for_ETH = web3_connection.fromWei(balance,'ether')
    print(balance_for_ETH)
Here is the answer to running this source code using the etherium address:
(web3_001) [mythcat@fedora PythonProjects]$ vi web3_func_001.py
(web3_001) [mythcat@fedora PythonProjects]$ python
Python 3.9.13 (main, Aug 25 2022, 23:26:10)
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from web3_func_001 import *
>>> is_connected()
True
>>> latest_block()
15516253
>>> balanceETH("0x74E55f28a8A0158b466FcB481EC7e6bE45D1DB91")
0
Since it is a rather complex field, I will come back with other tutorials when I have the necessary resources.

Monday, August 15, 2022

Blender 3D and python scripting - part 025.

In this tutorial I will show a simple python script for add a String interface to the Group Output.
You can see in the next image the result of this script.
You need to have an modifier Geometry Nodes or add new one.
This is the source script with comments for each step I used:
import bpy

#get active object - default 
obj = bpy.context.active_object

# set the default start for working with Geometry Nodes modifier
# you need to have a Geometry Nodes modifier
node_group = obj.modifiers['GeometryNodes'].node_group
nodes = node_group.nodes

#get the node named 'Group Output'
geom_out = nodes.get('Group Output')

#create a string node 
string_node = nodes.new('FunctionNodeInputString')
# set the name to 'String'
string_out = string_node.outputs['String']
# set the value to "This is a string"
string_node.string = "This is a string"

# link to the Group Output
node_group.links.new(string_out, geom_out.inputs[-1])

Sunday, August 14, 2022

Python : Install python with winget.

The winget command line tool enables users to discover, install, upgrade, remove and configure applications on Windows 10 and Windows 11 computers. This tool is the client interface to the Windows Package Manager service.
The winget can be installed on Windows 11 from the Windows Store.
More about winget command can be read on the Microsoft website.
After installation you can use the PowerShell command shell to install the python with this command:
winget install --id Python.Python.3 -e --source winget
Found Python 3 [Python.Python.3] Version 3.10.6
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Downloading https://www.python.org/ftp/python/3.10.6/python-3.10.6-amd64.exe
  ██████████████████████████████  27.5 MB / 27.5 MB
Successfully verified installer hash
Starting package install...
Successfully installed

Wednesday, August 10, 2022

Python 3.10.7 : Manim python package - part 001.

Manim is an engine for precise programmatic animations, designed for creating explanatory math videos, see the official GitHub repo.
Let's install this python package.
python.exe -m pip install manim
...
Successfully installed Pillow-9.2.0 Pygments-2.12.0 certifi-2022.6.15 charset-no
rmalizer-2.1.0 click-8.1.3 click-default-group-1.2.2 cloup-0.13.1 colour-0.1.5 c
ommonmark-0.9.1 decorator-5.1.1 glcontext-2.3.6 idna-3.3 isosurfaces-0.1.0 manim
-0.16.0.post0 manimpango-0.4.1 mapbox-earcut-0.12.11 moderngl-5.6.4 moderngl-win
dow-2.4.1 multipledispatch-0.6.0 networkx-2.8.5 pycairo-1.21.0 pydub-0.25.1 pygl
et-1.5.26 pyrr-0.10.3 requests-2.28.1 rich-12.5.1 scipy-1.9.0 screeninfo-0.8 ski
a-pathops-0.7.2 srt-3.5.2 tqdm-4.64.0 urllib3-1.26.11 watchdog-2.1.9 
You need to install the ffmepg software and add this to the environment path.
The default source code for create a circle is this:
from manim import *

# a simple python class
class DefaultClassExample(Scene):
    def construct(self):
        # add a circle 
        circle = Circle()
        # create a animation 
        self.play(Create(circle))
Use this command to create a video with this source code
manim -pql manim_001.py test
Manim Community v0.16.0.post0
...
INFO     Previewed File at: 'C:\Python310\media\videos\manim_001\480p15\DefaultClassExample.mp4'
...
This is the result of this command:

Blender 3D and python scripting - part 024.

In this tutorial I will show you how to use GeometryNodes with python script and Blender A.P.I.
You can see the result in the next image.
The Object Info node gets information from objects. This can be useful to control parameters in the geometry node tree with an external object, either directly by using its geometry, or via its transformation properties. An Object Info node can be added quickly by dragging an object into the node editor.
Another information can be found on the manual link.
In the next script you can see I created a simple BezierCurve object.
The definition named new_GeometryNodes_group is used to create two nodes GroupInit and GroupOutput.
I commente the source code to see some steps.
# import python packages
import bpy
from mathutils import Vector

# create a simpple BezierCurve and rename it with 'BezierCurveGeormetryNode'
bpy.ops.curve.primitive_bezier_curve_add()
bpy.ops.object.modifier_add(type='NODES')  

curve = bpy.context.active_object
curve.name = 'BezierCurveGeormetryNode'

# define a function for GroupInit and GroupOutput
def new_GeometryNodes_group():
    ''' Create a new empty node group that can be used
        in a GeometryNodes modifier.
    '''
    node_group = bpy.data.node_groups.new('GeometryNodes', 'GeometryNodeTree')
    inNode = node_group.nodes.new('NodeGroupInput')
    inNode.outputs.new('NodeSocketGeometry', 'Geometry')
    outNode = node_group.nodes.new('NodeGroupOutput')
    outNode.inputs.new('NodeSocketGeometry', 'Geometry')
    node_group.links.new(inNode.outputs['Geometry'], outNode.inputs['Geometry'])
    # the -3.5 is value for how far will be set the GroupInit and GroupOutput in the area of GeormetryNodes
    inNode.location = Vector((-3.5*inNode.width, 0))
    outNode.location = Vector((3.5*outNode.width, 0))
    return node_group

# the default curve modifier has no node group set, you need to set :
if curve.modifiers[-1].node_group:
    node_group = curve.modifiers[-1].node_group    
else:
    node_group = new_GeometryNodes_group()
    curve.modifiers[-1].node_group = node_group

# set default grup node as nodes
nodes = node_group.nodes

# get both nodes for each one 
group_in = nodes.get('Group Input')
group_out = nodes.get('Group Output')

# add the GeometryNodeObjectInfo to the GeometryNode area 
new_node_obj = nodes.new('GeometryNodeObjectInfo')
new_node_obj.inputs[0].default_value = bpy.data.objects["BezierCurveGeormetryNode"]

Monday, August 8, 2022

Python 3.10.4 : EbookLib python library - part 001.

EbookLib is a Python library for managing EPUB2/EPUB3 files. It’s capable of reading and writing EPUB files programmatically.
You can read more about this python library on this website.
First, I install with the pip tool:
C:\Python310>python -m pip install --upgrade pip
Requirement already satisfied: pip in c:\python310\lib\site-packages (22.1)
Collecting pip
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, stat
us=None)) after connection broken by 'NewConnectionError(': Failed to establish a n
ew connection: [Errno 11001] getaddrinfo failed')': /packages/1f/2c/d9626f045e7b
49a6225c6b09257861f24da78f4e5f23af2ddbdf852c99b8/pip-22.2.2-py3-none-any.whl
  Downloading pip-22.2.2-py3-none-any.whl (2.0 MB)
     ---------------------------------------- 2.0/2.0 MB 1.5 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.1
    Uninstalling pip-22.1:
      Successfully uninstalled pip-22.1
Successfully installed pip-22.2.2

C:\Python310>python -m pip install lxml-4.9.0-cp310-cp310-win_amd64.whl
Processing c:\python310\lxml-4.9.0-cp310-cp310-win_amd64.whl
Installing collected packages: lxml
Successfully installed lxml-4.9.0

C:\Python310>python -m pip install EbookLib --user
Collecting EbookLib
  Using cached EbookLib-0.17.1.tar.gz (111 kB)
  Preparing metadata (setup.py) ... done
Requirement already satisfied: lxml in c:\python310\lib\site-packages (from Eboo
kLib) (4.9.0)
Collecting six
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Using legacy 'setup.py install' for EbookLib, since package 'wheel' is not insta
lled.
Installing collected packages: six, EbookLib
  Running setup.py install for EbookLib ... done
Successfully installed EbookLib-0.17.1 six-1.16.0
I used a the simple python script from the last tutorial to test it:
The last tutorial used the default script from the official webpage.
Compared to the default script, I made changes, the selection in the Romanian language and diacritics...
from ebooklib import epub

book = epub.EpubBook()

# set metadata
book.set_identifier('__1976')
book.set_title('')
book.set_language('en')

book.add_author('Autho: Cătălin George Feștilă')

# create chapter
cap001 = epub.EpubHtml(title='Intro', file_name='capitolul_01.xhtml', lang='ro')
cap001.content=u'<h1>Introducere</h1><p>Această carte este...</p>'

# add chapter
book.add_item(cap001)

# define Table Of Contents
book.toc = (epub.Link('capitolul_01.xhtml', 'Introducere', 'introducere'),
             (epub.Section('O carte simplă'),
             (cap001, ))
            )

# add default NCX and Nav file
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())

# define CSS style
style = 'BODY {color: white;}'
nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)

# add CSS file
book.add_item(nav_css)

# basic spine
book.spine = ['nav', cap001]

# write to the file
epub.write_epub('ro_lan_test.epub', book, {})
This is the result of the epub file:

Sunday, July 24, 2022

Python 3.11.0a7 : image conversions in Python.

Image processing is very important in development, therefore also in python.
The image processing packages used in python have undergone changes over time.
Pillow and PIL cannot co-exist in the same environment. Before installing Pillow, please uninstall PIL.
Pillow higher than version 10 no longer supports import Image. Please use from PIL import Image instead.
Pillow higher than version 2.1.0 no longer supports import _imaging. Please use from PIL.Image import core as _imaging instead.
Although the image formats are old compared to the newer ones in vector format, they are preferred depending on the field of work.
You can see all of these file image formats on the official python package.
First, the basic installation start with upgrade the pip tool:
C:\PythonProjects\ConvertImages>python -m pip install --upgrade pip
Requirement already satisfied: pip in c:\python311alpha\lib\site-packages (22.1.
2)
Collecting pip
  Downloading pip-22.2-py3-none-any.whl (2.0 MB)
     ---------------------------------------- 2.0/2.0 MB 3.3 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.1.2
    Uninstalling pip-22.1.2:
      Successfully uninstalled pip-22.1.2
Successfully installed pip-22.2
Secondary, the install of the Pillow python package:
C:\PythonProjects\ConvertImages>python  -m pip install --upgrade Pillow
Collecting Pillow
  Downloading Pillow-9.2.0-cp311-cp311-win_amd64.whl (3.3 MB)
     ---------------------------------------- 3.3/3.3 MB 4.0 MB/s eta 0:00:00
Installing collected packages: Pillow
Successfully installed Pillow-9.2.0
I have a webp image here that I have scaled to smaller sizes and that I will test its conversion from webp format to png format.
The image is named waterski2 and I'm using the 3.11.0a7 python version.
Let's see the source code:
from PIL import Image

# open the image file WEBP
image = Image.open('waterski2.webp')

# show the image 
image.show()

# convert the image to RGB color
image = image.convert('RGB')

# save PNG RGB image
image.save('waterski2_RGB_PNG.png', 'png')

# save JPG RGB image
image.save('waterski2_RGB_JPG.jpg', 'jpeg')

# open the image file JPG
image_jpg = Image.open('waterski2_RGB_JPG.jpg')

# convert the image to RGB color
image_rgb_jpg = image_jpg.convert('RGB')

# save PNG RGB image from JPG
image_rgb_jpg.save('new-image_RGB_PNG_from_JPG.png', 'png')

#same process of conversion to WEBP file type
# from png image
image = Image.open('waterski2_RGB_PNG.png')
image = image.convert('RGB')
image.save('new-image_RGB_WEBP_from_png.webp', 'webp')
# from jpg image
image = Image.open('waterski2_RGB_JPG.jpg')
image = image.convert('RGB')
image.save('new-image_RGB_WEBP_from_jpg.webp', 'webp')
Here is a screenshot with these converted images and the processed image.

Saturday, July 23, 2022

Blender 3D and python scripting - part 023.

This script will add a submenu to the main Help menu with an icon with a folder that will open the explorer from the Windows operating system.
I have not solved the tooltip of this button, it is set to show a message about opening a URL.
This is the source code:
import bpy

def menu_func(self, context):
    '''Open explorer in windows systems'''
    self.layout.operator(
            "wm.url_open", text="Open explorer", icon='FILE_FOLDER').url = "C:/"
def register():
    bpy.types.TOPBAR_MT_help.append(menu_func)

def unregister():
    bpy.types.TOPBAR_MT_help.remove(menu_func)

if __name__ == "__main__":
    register()
If you want to change the tooltip then need to create a class OpenOperator and wrapper for this operator function and set the bl_label and all you need to have it.
The menu_func will get the layout operator with all defined in the class OpenOperator and will set the tooltip with the text: Open explorer in windows systems.
See this new source code:
import bpy

def menu_func(self, context):
    self.layout.operator(
            OpenOperator.bl_idname, text="Open explorer", icon='FILE_FOLDER')

class OpenOperator(bpy.types.Operator):
    """Open explorer in windows systems"""
    bl_idname = "wm.open_explorer"
    bl_label = "Open explorer"

    def execute(self, context):
        bpy.ops.wm.url_open(url="C:/")
        return {'FINISHED'}


def register():
    bpy.utils.register_class(OpenOperator)
    bpy.types.TOPBAR_MT_help.append(menu_func)


def unregister():
    bpy.utils.unregister_class(OpenOperator)
    bpy.types.TOPBAR_MT_help.remove(menu_func)


if __name__ == "__main__":
    register()