rashid01 commited on
Commit
02183c7
·
verified ·
1 Parent(s): 0417a77

Create q3.py

Browse files
Files changed (1) hide show
  1. q3.py +55 -0
q3.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+
3
+ def summarize_text(api_key, long_text, model="text-davinci-003", max_tokens=150, temperature=0.3):
4
+ """
5
+ Summarize a long piece of text using LangChain (GPT-3 model).
6
+
7
+ Parameters:
8
+ - api_key (str): Your OpenAI API key.
9
+ - long_text (str): The long piece of text to be summarized.
10
+ - model (str): The GPT-3 model to use. Defaults to 'text-davinci-003'.
11
+ - max_tokens (int): Maximum number of tokens (words) in the generated summary text. Defaults to 150.
12
+ - temperature (float): Controls randomness in text generation. Higher values make the output more diverse. Defaults to 0.3.
13
+
14
+ Returns:
15
+ - summarized_text (str): The summarized text.
16
+ """
17
+ # Set the OpenAI API key
18
+ openai.api_key = api_key
19
+
20
+ # Construct prompt for text summarization
21
+ prompt = f"Summarize the following text:\n{long_text}\n\nSummary:"
22
+
23
+ # Generate text completion using LangChain
24
+ response = openai.Completion.create(
25
+ engine=model,
26
+ prompt=prompt,
27
+ max_tokens=max_tokens,
28
+ temperature=temperature
29
+ )
30
+
31
+ # Extract and return the summarized text
32
+ summarized_text = response['choices'][0]['text'].strip()
33
+ return summarized_text
34
+
35
+ # Example usage:
36
+ def main():
37
+ # Replace 'your_openai_api_key_here' with your actual API key
38
+ api_key = 'your_openai_api_key_here'
39
+
40
+ # Example long piece of text
41
+ long_text = """
42
+ The Hubble Space Telescope has been one of the most important tools in astronomy since its launch in 1990. It has helped scientists make numerous discoveries about our universe, from the age of the universe to the existence of dark matter.
43
+ Over its decades of operation, Hubble has captured stunning images of galaxies, nebulae, and other cosmic phenomena. Its observations have led to breakthroughs in understanding the formation and evolution of stars and galaxies.
44
+ Despite its initial issues with a flawed mirror, which were later corrected by space shuttle missions, Hubble continues to be a vital asset to astronomers around the world. Its successor, the James Webb Space Telescope, is set to launch soon and is expected to expand on Hubble's discoveries.
45
+ """
46
+
47
+ # Summarize the text
48
+ summarized_text = summarize_text(api_key, long_text)
49
+
50
+ # Print summarized text
51
+ print("Summarized Text:")
52
+ print(summarized_text)
53
+
54
+ if __name__ == "__main__":
55
+ main()