Monday, August 28, 2023

Using ChatGPT with Python GUI


# API Key created on 28th August, 2023, valid for 3 months

# Email: bt25.bcrs@gmail.com Password: Bantuda25New

# Verification Mobile: 9847578833

# https://copyassignment.com/create-your-own-chatgpt-with-python/


import openai

from tkinter import *


win = Tk()

win.geometry("750x450")

win['background'] = '#11A37B'

win.title("Using ChatGPT with Python GUI")


openai.api_key = "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"

model_engine = "text-davinci-003"


def reply(e):

  completion = openai.Completion.create(n=1, stop=None,

  engine="text-davinci-003", prompt=S1.get(),

  max_tokens=1024, temperature=0.5)

  response = completion.choices[0].text.strip()

  txt.delete("0.0", END)

  txt.insert("1.0", response)


S1, S2 = StringVar(), StringVar()

S1.set("")

font1 = ("Verdana", 14, "bold")

font2 = ("Verdana", 24, "bold")

lblHeader = Label(win, text="Hosted by BCRS", font=font2, bg='#11A37B', fg='#FFFFFF')

lblHeader.place(x=280, y=25)

pic = PhotoImage(file="ChatGPT.png")

lblImg = Label(win, image=pic)

lblImg.place(x=40, y=5)

lbl = Label(win, text='Enter prompt', font=font1, bg='#11A37B', fg='#FFFFFF')

lbl.place(x=10, y=130)

ent = Entry(win, textvariable=S1, font=font1, width=40, bg='#11A37B', fg='#FFFFFF')

ent.place(x=180, y=130)

ent.bind('<Return>', reply)

ent.focus()

txt = Text(win, font=font1, width=50, height=10, wrap=WORD, bg='#11A37B', fg='#FFFFFF')

txt.place(x=25, y=180)


win.mainloop()


Use ChatGPT with Python

# API Key created on 28th August, 2023, valid for 3 months

# Email: bt25.bcrs@gmail.com Password: Bantuda25New

# Verification Mobile: 9847578833

# https://copyassignment.com/create-your-own-chatgpt-with-python/


import openai


openai.api_key = "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"


while True:

  model_engine = "text-davinci-003"

  prompt = input('Enter new prompt: ')


  if 'exit' in prompt or 'quit' in prompt:

    break


  completion = openai.Completion.create(

    engine=model_engine, prompt=prompt,

    max_tokens=1024, n=1, stop=None,

    temperature=0.5,

  )


  response = completion.choices[0].text

  print(response)


Tuesday, August 15, 2023

Python script to backup files

import shutil

inn = open("C:/Users/TECHNOSOFT/Desktop/Files to Backup.txt")
L = inn.readlines()
L1 = []
for src in L:
  src = src.strip()
  dest = "G" + src[1:]
  shutil.copy(src, dest)
inn.close()

Saturday, August 5, 2023

Calculating percentiles of marks (roughly as per CBSE guidelines)

import random, csv


nos = 135

nop = 8

percentile = nos // nop

grades = ["A+", "A", "B+", "B", "C+", "C", "D", "F",]


def WriteCSV():

  out = open("Marks.csv", 'w', newline='')

  W = csv.writer(out)

  W.writerow(["English", "Maths"])

  for i in range(nos):

    E = random.randint(30, 100)

    M = random.randint(30, 100)

    W.writerow([E, M])

  out.close()


def ReadCSV():

  inn = open("Marks.csv", 'r', newline='')

  R = csv.reader(inn)

  L = list(R)

  eMarks, mMarks = [], []

  eCutOffs, mCutOffs = [], []

  for i in range(1, len(L)):

    eMarks.append(int(L[i][0]))

    mMarks.append(int(L[i][1]))

  eMarks.sort(reverse=True)

  mMarks.sort(reverse=True)

  for i in range(0, len(eMarks), percentile):

    print(eMarks[i:i+percentile])

    if i < nop * percentile:

      eCutOffs.append(eMarks[i+percentile])

  print(eCutOffs)

  for i in range(1, len(L)):

    for j in range(len(eCutOffs)):

      if int(L[i][0]) >= eCutOffs[j]:

        grade = grades[j]

    L[i].insert(1, grade)

  print(L)


  out = open("Grades.xlsx", 'w', newline='')

  W = csv.writer(out)

  W.writerows(L)

  out.close()


WriteCSV()

ReadCSV()

Saturday, July 22, 2023

BANNER program in Python

# If you've used Unix, you might be familiar with the Banner command.
# Here is my version of the Banner command on Python 3. 
# Sample Output

alpha = [[[" AAAAA "],
          ["A     A"],
          ["A     A"],
          ["AAAAAAA"],
          ["A     A"],
          ["A     A"],
          ["A     A"]
         ], 
         [["BBBBBB "], 
          ["B     B"],
          ["B     B"],
          ["BBBBBB "],
          ["B     B"],
          ["B     B"],
          ["BBBBBB "]
         ],
         [[" CCCCC "],
          ["C     C"],
          ["C      "],
          ["C      "],
          ["C      "],
          ["C     C"],
          [" CCCCC "]
         ],
         [["DDDDDD "], 
          ["D     D"],
          ["D     D"],
          ["D     D"],
          ["D     D"],
          ["D     D"],
          ["DDDDDD "],
         ],
         [["EEEEEEE"], 
          ["E      "],
          ["E      "],
          ["EEEEE  "],
          ["E      "],
          ["E      "],
          ["EEEEEEE"],
         ],
         [["FFFFFFF"],
          ["F      "],
          ["F      "],
          ["FFFFF  "],
          ["F      "],
          ["F      "],
          ["F      "],
         ],
         [[" GGGGGG"], 
          ["G      "],
          ["G      "],
          ["G   GGG"],
          ["G   G G"],
          ["G   G G"],
          [" GGGG G"],
         ],
         [["H     H"], 
          ["H     H"],
          ["H     H"],
          ["HHHHHHH"],
          ["H     H"],
          ["H     H"],
          ["H     H"],
         ],
         [["IIIIIII"], 
          ["   I   "],
          ["   I   "],
          ["   I   "],
          ["   I   "],
          ["   I   "],
          ["IIIIIII"],
         ],
         [["JJJJJJJJ"], 
          ["    J   "],
          ["    J   "],
          ["    J   "],
          ["    J   "],
          ["J   J   "],
          [" JJJ    "],
         ],
         [["K   K"], 
          ["K  K "],
          ["K K  "],
          ["KK   "],
          ["K K  "],
          ["K  K "],
          ["K   K"],
         ],
         [["L      "], 
          ["L      "],
          ["L      "],
          ["L      "],
          ["L      "],
          ["L      "],
          ["LLLLLLL"],
         ],
         [["M     M"], 
          ["MM   MM"],
          ["M M M M"],
          ["M  M  M"],
          ["M     M"],
          ["M     M"],
          ["M     M"],
         ],
         [["N     N"],
          ["NN    N"],
          ["N N   N"],
          ["N  N  N"],
          ["N   N N"],
          ["N    NN"],
          ["N     N"],
         ],
         [[" OOOOO "], 
          ["O     O"],
          ["O     O"],
          ["O     O"],
          ["O     O"],
          ["O     O"],
          [" OOOOO "],
         ],
         [["PPPPPP "], 
          ["P     P"],
          ["P     P"],
          ["PPPPPP "],
          ["P      "],
          ["P      "],
          ["P      "],
         ],
         [[" QQQQQ   "], 
          ["Q     Q  "],
          ["Q     Q  "],
          ["Q   Q Q  "],
          ["Q    QQ  "],
          ["Q     Q  "],
          [" QQQQQ Q "],
         ],
         [["RRRRRR "], 
          ["R     R"],
          ["R     R"],
          ["RRRRRR "],
          ["R   R  "],
          ["R    R "],
          ["R     R"],
         ],
         [[" SSSSS "], 
          ["S     S"],
          ["S      "],
          [" SSSSS "],
          ["      S"],
          ["S     S"],
          [" SSSSS "],
         ],
         [["TTTTTTT"], 
          ["   T   "],
          ["   T   "],
          ["   T   "],
          ["   T   "],
          ["   T   "],
          ["   T   "],
         ],
         [["U     U"], 
          ["U     U"],
          ["U     U"],
          ["U     U"],
          ["U     U"],
          ["U     U"],
          [" UUUUU "],
         ],
         [["V      V"],
          ["V      V"],
          ["V      V"],
          [" V    V "],
          [" V   V  "],
          ["  V V   "],
          ["   V    "],
         ],
         [["W     W"], 
          ["W     W"],
          ["W     W"],
          ["W  W  W"],
          ["W W W W"],
          ["WW   WW"],
          ["W     W"],
         ],
         [["X     X"], 
          [" X   X "],
          ["  X X  "],
          ["   X   "],
          ["  X X  "],
          [" X   X "],
          ["X     X"],
         ],
         [["Y     Y"], 
          [" Y   Y "],
          ["  Y Y  "],
          ["   Y   "],
          ["   Y   "],
          ["   Y   "],
          ["   Y   "],
         ],
         [["ZZZZZZZ"], 
          ["     Z "],
          ["    Z  "],
          ["   Z   "],
          ["  Z    "],
          [" Z     "],
          ["ZZZZZZZ"],
         ]
        ]

name = input("Enter a string: ").upper()
for i in range(7):
  for j in range(len(name)):
    diff = ord(name[j]) - 65
    print(*alpha[diff][i], end='    ')
  print()

Monday, July 17, 2023

Removing blank lines from Blogger blogs

with open("Blogger.txt", 'r') as inn:    # Text copied onto Blogger.txt 

  L = inn.readlines()

  L1 = []

  for item in L:

    if item != '\n':

      L1.append(item)


with open("Blogger.txt", 'w') as out:    # Write back to Blogger.txt

  for item in L1:

    out.write(item)



Vigenere Cipher

def CaesarEncrypt(ch, shift):

  ch1 = ord(ch) + shift

  if ch1 > 122:

    ch1 -= 26

  return chr(ch1)


def CaesarDecrypt(ch, shift):

  ch1 = ord(ch) - shift

  if ch1 < 97:

    ch1 += 26

  return chr(ch1)


def VigenereEncrypt():

  global encryptedMessage

  for i in range(len(originalMessage)):

    if originalMessage[i] == ' ':

      encryptedMessage += ' '

      continue

    shift = ord(key[i%len(key)]) - ord('A'.lower())

    char = CaesarEncrypt(originalMessage[i], shift)

    encryptedMessage += char

  print(encryptedMessage)


def VigenereDecrypt():

  global decryptedMessage

  for i in range(len(encryptedMessage)):

    if encryptedMessage[i] == ' ':

      decryptedMessage += ' '

      continue

    shift = ord(key[i%len(key)]) - ord('A'.lower())

    char = CaesarDecrypt(encryptedMessage[i], shift)

    decryptedMessage += char

  print(decryptedMessage)


originalMessage = "attacking tonight"

encryptedMessage, decryptedMessage = "", ""

key = "OCULORHINOLARINGOLOGY".lower()

size = len(key)

VigenereEncrypt()

VigenereDecrypt()