tranny / App /Chat /utils /Dev /PalmAPI.py
Mbonea's picture
summarization improved
4e0c974
raw
history blame
No virus
10 kB
import aiohttp
import asyncio
import google.generativeai as palm
from langchain.llms import GooglePalm
from langchain.chains.summarize import load_summarize_chain
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain import PromptTemplate
import os
PALM_API = ""
API_KEY=os.environ.get("PALM_API",PALM_API)
palm.configure(api_key=API_KEY)
llm = GooglePalm(google_api_key=API_KEY, safety_settings= [
{"category": "HARM_CATEGORY_DEROGATORY", "threshold": 4},
{"category": "HARM_CATEGORY_TOXICITY", "threshold": 4},
{"category": "HARM_CATEGORY_VIOLENCE", "threshold": 4},
{"category": "HARM_CATEGORY_SEXUAL", "threshold": 4},
{"category": "HARM_CATEGORY_MEDICAL", "threshold": 4},
{"category": "HARM_CATEGORY_DANGEROUS", "threshold": 4},
],)
text_splitter = RecursiveCharacterTextSplitter(separators=["\n\n", "\n"], chunk_size=10000, chunk_overlap=500)
summary_chain = load_summarize_chain(llm=llm, chain_type='map_reduce',
# verbose=True # Set verbose=True if you want to see the prompts being used
)
essay= ''' TFC Mamma Ron Subway Galito Urban Heart Kootie Java Square In this video, I'm going to try every single fast food chain in Irobi Kenya and I'm going to rate them on a scale of terrible, bad, mid, good and for the incredible ones, go zest! I've broken them up into categories, so pizza category, burger category, chicken, general fast food and breakfast category and I'm starting with Pizza Hut To keep this fair across all restaurants, I'm ordering the cheapest possible meal on the menu or as close to my budget of 500 Kenya shillings and for that price in Pizza Hut Okay, so this is the mine meat lovers pizza This is going to be my first tasting of Pizza Hut in Irobi Kenya I haven't washed my hands, no one has to know that Okay, I could already feel how chunky this pizza is Maybe dip that in this barbecue sauce Mmm Okay, '''
docs = text_splitter.create_documents([essay])
# print(docs[0].page_content)
map_prompt = """
Write a concise summary of the following:
"{text}"
CONCISE SUMMARY:
"""
combine_prompt = """
Write a concise summary of the following text delimited by triple backquotes.
Return your response in bullet points which covers the key points of the text.
```{text}```
BULLET POINT SUMMARY:
"""
combine_prompt_template = PromptTemplate(template=combine_prompt, input_variables=["text"])
map_prompt_template = PromptTemplate(template=map_prompt, input_variables=["text"])
summary_chain = load_summarize_chain(llm=llm,
chain_type='map_reduce',
map_prompt=map_prompt_template,
combine_prompt=combine_prompt_template,
verbose=True
)
output = summary_chain.run(docs)
print(output)
def count_tokens(text):
return palm.count_message_tokens(prompt=text)['token_count']
async def summarization(text):
url = f"https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key={API_KEY}"
headers = {
"Content-Type": "application/json",
}
data = {
"prompt": {
"text": f"### You are given the an audio transcript as context. You are required to write a summary, list down the main takeaways, also cite the text when necessary.\n<input> \"There's actually a military proven technique to fall asleep in exactly two minutes after closing your eyes. It's mind-blowing. Here's how you can do it too. Now, this technique was developed in the military to allow soldiers to fall asleep at any time, any place, even on the battlefield when the environment is extremely uncomfortable and there's a lot of noise happening. Sleep for a soldier is crucial. Now, according to my research, this was developed mainly for fighter pilots who need 100% of their reflexes and focus, which we all know decreases with the lack of sleep. So here's the technique that they use, and it's quite simple. First, you need to calm your body and systematically relax and shut down each part of your body from head to toe, literally. Start by relaxing the muscles in your forehead. Relax your eyes, your cheeks, your jaw, and focus on your breathing. Now go down to your neck and your shoulders. Make sure your shoulders are not tensed up, drop them as low as you can, and keep your arms loose to your side, including your hands and fingers. Imagine this warm sensation going from your head all the way down to your fingertips. Now take a deep breath and slowly exhale, relaxing your chest, your stomach, down to your thighs, knees, legs, and feet. Again, imagine this warm sensation going down from your heart all the way to your toes. Now while you're doing this, it's really important to clear your mind of any stresses. To do this, think of two scenarios. One, you're lying in a canoe on a calm lake with nothing but a clear blue sky above you. Two, you're lying in a black velvet hammock in a pitch black room. At any time when you start thinking of anything else or you start getting distracted, repeat these words for 10 seconds. Don't think. Don't think. Don't think. So that's the technique. You're supposed to practice every night for six weeks. 96% of people who master this technique are actually able to fall asleep within two minutes of shutting their eyes. I find it super interesting. I did not invent this technique, but I'm definitely going to try it out. Let me know if you're on board as well.\"\n</input>\n<output>\nSummary: The military developed a technique to help soldiers fall asleep at any time, any place. This technique involves systematically relaxing and shutting down each part of the body from head to toe, while clearing the mind of any stresses.\n</output>\n\n\n\n <input> {text} </input>"
},
"temperature": 0.95,
"top_k": 100,
"top_p": 0.95,
"candidate_count": 1,
"max_output_tokens": 1024,
"stop_sequences": ["</output>"],
"safety_settings": [
{"category": "HARM_CATEGORY_DEROGATORY", "threshold": 4},
{"category": "HARM_CATEGORY_TOXICITY", "threshold": 4},
{"category": "HARM_CATEGORY_VIOLENCE", "threshold": 4},
{"category": "HARM_CATEGORY_SEXUAL", "threshold": 4},
{"category": "HARM_CATEGORY_MEDICAL", "threshold": 4},
{"category": "HARM_CATEGORY_DANGEROUS", "threshold": 4},
],
}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data, headers=headers) as response:
if response.status == 200:
result = await response.json()
print(result)
temp = result["candidates"][0]["output"]
return temp
else:
print(f"Error: {response.status}\n{await response.text()}")
def generate_summary(data):
template = """ {data} """.format(data=data)
chunk_size = 30_000
inc = 250
max_tokens = 7000
min_tokens = 6000
max_token_doc = 0
# choose the appropriate chunk size
while True:
# initialize text splitter
text_splitter = RecursiveCharacterTextSplitter(
separators=["\n\n", "\n"],
chunk_size=chunk_size,
chunk_overlap=0,
)
docs = text_splitter.create_documents([data])
temp =[]
for doc in docs:
temp_tokens=count_tokens(doc.page_content)
temp.append(temp_tokens)
max_token_doc = max(temp)
docs_count=len(temp)
# print(docs[0].page_content)
if docs_count ==1:
break
if max_tokens < max_token_doc or max_token_doc < min_tokens:
if max_tokens < max_token_doc:
chunk_size -= inc
else:
chunk_size += inc
continue
else:
break
return docs
async def main():
docs=generate_summary(''' <input>
Transcript
Yo, Mabu, you really the only independent artist putting up numbers right now, bro They call me the independent variable for a reason gang. Oh, that's why I spent eight hours a day in school And I still put up more numbers than these fleas Like most of our rappers in Santa the streets only dealers who took was a plea I'm using the police, so this is a match that I'm a PD I cover my rhymes, I'm an innocent creep, keep my daily back when I sleep I'm rapping the words, but they write it for me Me, I'm all about keeping the peace, I'm me at least I get paid because a lot of these rhymes make up for free They call me the duck, I got the spreadsheet shot, but say, can I bring a friend? Never, never, never I guess she forgot the bad light, the two fives don't equal a ten Quick math, don't try it again, she's shaped like a turtle Or a hen, her makeup is fucked, she don't know how to blend, make her do worldle She did it her brains if she given her head, call her Virgil Cause the way she be blind shit got me dead She had a gut arc ten, I want it in a second circle She like Babu, I like purple, so I blew her back out when I left her on red Get it, cause blue and red equals purple So I blew her back out when I left her on red, don't let that go over your head One thought, two thought, three thought four They offered their knees on the floor, Keisha Becky, Sophie, so Texted me begging for mob, black in their bitches, she calling me bro If you're my dog then throw my bone Ruff, ruff, throw my booty, Keisha's in a flow To all my competition, take a grip, take a grip I invested money in myself and it paid, I can take a break too My boy is a household name</input>''' )
for doc in docs:
summary=await summarization(doc.page_content)
print(summary)
# if __name__ == '__main__':
# asyncio.run(main=main())