Mbonea commited on
Commit
cdef94b
·
1 Parent(s): 0fe435d
Files changed (1) hide show
  1. App/Chat/utils/Summarize.py +67 -58
App/Chat/utils/Summarize.py CHANGED
@@ -1,17 +1,23 @@
1
  import aiohttp
2
- import asyncio,pprint
3
  import google.generativeai as palm
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
  from langchain import PromptTemplate
6
  import os
7
- PALM_API = ''
8
- API_KEY=os.environ.get("PALM_API",PALM_API)
9
- palm.configure(api_key=API_KEY)
 
10
 
11
 
 
 
 
12
 
13
 
14
- text_splitter = RecursiveCharacterTextSplitter(separators=["\n\n", "\n","."], chunk_size=40_000, chunk_overlap=500)
 
 
15
 
16
 
17
  map_prompt = """
@@ -29,69 +35,72 @@ Return your response in a detailed verbose paragraph which covers the text. Make
29
 
30
  SUMMARY:
31
  """
32
- def count_tokens(text):
33
- return palm.count_message_tokens(prompt=text)['token_count']
34
-
35
-
36
- async def PalmTextModel(text,candidates=1):
37
- url = f"https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key={API_KEY}"
38
-
39
- headers = {
40
- "Content-Type": "application/json",
41
- }
42
-
43
- data = {
44
- "prompt": {
45
- "text": text
46
- },
47
- "temperature": 0.95,
48
- "top_k": 100,
49
- "top_p": 0.95,
50
- "candidate_count": candidates,
51
- "max_output_tokens": 1024,
52
- "stop_sequences": ["</output>"],
53
- "safety_settings": [
54
- {"category": "HARM_CATEGORY_DEROGATORY", "threshold": 4},
55
- {"category": "HARM_CATEGORY_TOXICITY", "threshold": 4},
56
- {"category": "HARM_CATEGORY_VIOLENCE", "threshold": 4},
57
- {"category": "HARM_CATEGORY_SEXUAL", "threshold": 4},
58
- {"category": "HARM_CATEGORY_MEDICAL", "threshold": 4},
59
- {"category": "HARM_CATEGORY_DANGEROUS", "threshold": 4},
60
- ],
61
- }
62
-
63
-
64
- async with aiohttp.ClientSession() as session:
65
- async with session.post(url, json=data, headers=headers) as response:
66
- if response.status == 200:
67
- result = await response.json()
68
- # print(result)
69
- if candidates>1:
70
- temp = [candidate["output"] for candidate in result["candidates"]]
71
- return temp
72
- temp = result["candidates"][0]["output"]
73
- return temp
74
- else:
75
- print(f"Error: {response.status}\n{await response.text()}")
76
 
77
 
78
- async def Summarizer(essay):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
 
80
  docs = text_splitter.create_documents([essay])
81
 
82
- #for 1 large document
83
  if len(docs) == 1:
84
- tasks = [PalmTextModel(combine_prompt.format(text=doc.page_content)) for doc in docs]
 
 
85
  # Gather and execute the tasks concurrently
86
  responses = await asyncio.gather(*tasks)
87
- ans=" ".join(responses)
88
- return ans
89
 
90
  tasks = [PalmTextModel(map_prompt.format(text=doc.page_content)) for doc in docs]
91
  # Gather and execute the tasks concurrently
92
  responses = await asyncio.gather(*tasks)
93
- main=" ".join(responses)
94
- ans=await PalmTextModel(combine_prompt.format(text=main),candidates=1)
95
  return ans
96
-
97
-
 
1
  import aiohttp
2
+ import asyncio, pprint
3
  import google.generativeai as palm
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
  from langchain import PromptTemplate
6
  import os
7
+ from poe_api_wrapper import PoeApi
8
+ import pprint
9
+ client = PoeApi("sXvCnfYy8CHnXNTRlxhmVg==")
10
+ bot = "Assistant"
11
 
12
 
13
+ PALM_API = ""
14
+ API_KEY = os.environ.get("PALM_API", PALM_API)
15
+ palm.configure(api_key=API_KEY)
16
 
17
 
18
+ text_splitter = RecursiveCharacterTextSplitter(
19
+ separators=["\n\n", "\n", "."], chunk_size=40_000, chunk_overlap=500
20
+ )
21
 
22
 
23
  map_prompt = """
 
35
 
36
  SUMMARY:
37
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
+ def count_tokens(text):
41
+ return palm.count_message_tokens(prompt=text)["token_count"]
42
+
43
+
44
+ # async def PalmTextModel(text, candidates=1):
45
+ # url = f"https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key={API_KEY}"
46
+
47
+ # headers = {
48
+ # "Content-Type": "application/json",
49
+ # }
50
+
51
+ # data = {
52
+ # "prompt": {"text": text},
53
+ # "temperature": 0.95,
54
+ # "top_k": 100,
55
+ # "top_p": 0.95,
56
+ # "candidate_count": candidates,
57
+ # "max_output_tokens": 1024,
58
+ # "stop_sequences": ["</output>"],
59
+ # "safety_settings": [
60
+ # {"category": "HARM_CATEGORY_DEROGATORY", "threshold": 4},
61
+ # {"category": "HARM_CATEGORY_TOXICITY", "threshold": 4},
62
+ # {"category": "HARM_CATEGORY_VIOLENCE", "threshold": 4},
63
+ # {"category": "HARM_CATEGORY_SEXUAL", "threshold": 4},
64
+ # {"category": "HARM_CATEGORY_MEDICAL", "threshold": 4},
65
+ # {"category": "HARM_CATEGORY_DANGEROUS", "threshold": 4},
66
+ # ],
67
+ # }
68
+
69
+ # async with aiohttp.ClientSession() as session:
70
+ # async with session.post(url, json=data, headers=headers) as response:
71
+ # if response.status == 200:
72
+ # result = await response.json()
73
+ # # print(result)
74
+ # if candidates > 1:
75
+ # temp = [candidate["output"] for candidate in result["candidates"]]
76
+ # return temp
77
+ # temp = result["candidates"][0]["output"]
78
+ # return temp
79
+ # else:
80
+ # print(f"Error: {response.status}\n{await response.text()}")
81
+
82
+
83
+ async def PalmTextModel(message):
84
+ for chunk in client.send_message(bot, message,chatCode='TWVzc2FnZTozMDY2MzYwNjQ5OQ=='):
85
+ pass
86
+ return chunk["text"]
87
 
88
+ async def Summarizer(essay):
89
  docs = text_splitter.create_documents([essay])
90
 
91
+ # for 1 large document
92
  if len(docs) == 1:
93
+ tasks = [
94
+ PalmTextModel(combine_prompt.format(text=doc.page_content)) for doc in docs
95
+ ]
96
  # Gather and execute the tasks concurrently
97
  responses = await asyncio.gather(*tasks)
98
+ ans = " ".join(responses)
99
+ return ans
100
 
101
  tasks = [PalmTextModel(map_prompt.format(text=doc.page_content)) for doc in docs]
102
  # Gather and execute the tasks concurrently
103
  responses = await asyncio.gather(*tasks)
104
+ main = " ".join(responses)
105
+ ans = await PalmTextModel(combine_prompt.format(text=main), candidates=1)
106
  return ans