Skip to content

Forum in maintenance, we will back soon 🙂

Notifications
Clear all

token_limit.py

12 Posts
5 Users
3 Reactions
48 Views
(@hung-ng)
Posts: 4
Active Member Customer
Topic starter
 

Hi Hasan,

Why do you put the following token_limit.py code block in parentheses?

# Split transcript into chunks if needed
chunks = (
  helpers.split_text_into_chunks(video_transcript, helpers.max_tokens_per_chunk)
  if helpers.count_tokens(video_transcript, selected_model)
  > helpers.max_tokens_per_chunk
  else [video_transcript]
)

Thank you in advance.

 
Posted : 07/16/2023 6:25 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

This code block is using a Python feature called the ternary (or conditional) operator. The ternary operator is a shorthand way of writing an if-else statement. It allows you to evaluate a condition and return one value if the condition is true, and another value if the condition is false. Here's a breakdown:

  • helpers.count_tokens(video_transcript, selected_model) > helpers.max_tokens_per_chunk is the condition.
  • helpers.split_text_into_chunks(video_transcript, helpers.max_tokens_per_chunk) is the value that will be assigned to chunks if the condition is true.
  • [video_transcript] is the value that will be assigned to chunks if the condition is false.

The reason this code block is wrapped in parentheses is to improve readability. The parentheses make it clear that the entire expression is one statement. Without them, Python might misinterpret where the statement ends, especially when the ternary operator is part of a larger expression or if the code is formatted across multiple lines. It also makes it clear to anyone reading the code that this is a single logical operation, rather than multiple separate statements.

 
Posted : 07/17/2023 9:00 am
SSAdvisor reacted
(@kunal-lonhare)
Posts: 28
Eminent Member Customer
 

@admin hello sir I am facing this issue how to solve this in token limit?

 
Posted : 08/08/2023 12:04 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

Does the helper file contain max_token_per_chunk?

 
Posted : 08/09/2023 10:51 am
(@kunal-lonhare)
Posts: 28
Eminent Member Customer
 

@admin sir i have this code on helpers file

"import openai
import tiktoken
import newspaper
import youtube_transcript_api
import re
from youtube_transcript_api import YouTubeTranscriptApi

# replace with your api key
openai.api_key = "sk-Mt9bKcl5G0ePQxCqAT3BlbkFJZMrNYkzaesj0pQDO8iem"

def estimate_input_cost(model_name, token_count):
if model_name == "gpt-3.5-turbo-0613":
cost_per_1000_tokens = 0.0015
if model_name == "gpt-3.5-turbo-16k-0613":
cost_per_1000_tokens = 0.003
if model_name == "gpt-4-0613":
cost_per_1000_tokens = 0.03
if model_name == "gpt-4-32k-0613":
cost_per_1000_tokens = 0.06

estimated_cost = (token_count / 1000) * cost_per_1000_tokens
return estimated_cost

def count_tokens(text,selected_model):
encoding = tiktoken.encoding_for_model(selected_model)
num_tokens = encoding.encode(text)
return len(num_tokens)

def generate_text_with_openai(user_prompt):
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # you can replace this with your preferred model
messages=[{"role": "user", "content": user_prompt}],
)
return completion.choices[0].message.content

def get_article_from_url(url):
try:
# Scrape the web page for content using newspaper
article = newspaper.Article(url)
# Download the article's content with a timeout of 10 seconds
article.download()
# Check if the download was successful before parsing the article
if article.download_state == 2:
article.parse()
# Get the main text content of the article
article_text = article.text
return article_text
else:
print("Error: Unable to download article from URL:", url)
return None
except Exception as e:
print("An error occurred while processing the URL:", url)
print(str(e))
return None

def get_video_transcript(video_url):
match = re.search(r"(?:youtube\.com\/watch\?v=|youtu\.be\/)(.*)", video_url)
if match:
VIDEO_ID = match.group(1)
else:
raise ValueError("Invalid YouTube URL")

video_id = VIDEO_ID

# Fetch the transcript using the YouTubeTranscriptApi
transcript = YouTubeTranscriptApi.get_transcript(video_id)

# Extract the text of the transcript
transcript_text = ""
for line in transcript:
transcript_text += line["text"] + " "
return transcript_text"

Now please can you tell me what to do here?

This post was modified 1 year ago by Kunal Lonhare
 
Posted : 08/09/2023 2:45 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@kunal-lonhare where is "max_token_per_chunk"

you can use the code block here to paste source codes next time.

and make sure you are not sharing the real API key

 
Posted : 08/10/2023 11:26 am
(@gerald-piuk)
Posts: 2
New Member Customer
 

Hello Hassan, or others in the know! I have a problem with "token_limit.py" where I don't know what to do.

Here is the error message:

Traceback (most recent call last):
File "c:\Users\gpiuk\Documents\GitHub\prompts\token_limit.py", line 31, in <module>.
print(overall_output)
file "C:\Users\gpiuk\AppData\Local\Programs\Python\Python311\Lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: The codec 'charmap' cannot encode the character '\U0001f4a1' at position 2: The character corresponds to <undefined>.

It would be great if you could help me.

Greetings from Austria

Gerald

 
Posted : 09/13/2023 5:49 am
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@gerald-piuk can you please screenshot your code, or share it here, so we can check it.

 
Posted : 09/13/2023 7:13 am
(@gerald-piuk)
Posts: 2
New Member Customer
 

Hi Hassan,

THANK YOU for your quick offer of help!
For some reason this is working now, the bad thing is that I don't know why but it is working!

THANK YOU again and you are doing great!

Best regards from the alpine republic!

Gerald

 
Posted : 09/13/2023 1:27 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@gerald-piuk glad to hear that 🙂

 
Posted : 09/14/2023 9:34 am
Gerald reacted
(@shelaheramis)
Posts: 2
Member Registered
 

@admin it's not in the helper file

 
Posted : 09/30/2023 4:47 pm
(@shelaheramis)
Posts: 2
Member Registered
 

Posted by: @shelaheramis

@admin it's not in the helper file

never mind - it was there - I overlooked it. It sais max_tokens_per_chunk = 1000 

 

 
Posted : 09/30/2023 5:03 pm
Share:
[the_ad_group id="312"]