-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathai_summary_report.py
More file actions
62 lines (45 loc) Β· 2.43 KB
/
ai_summary_report.py
File metadata and controls
62 lines (45 loc) Β· 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# coding: utf-8
# Copyright (c) 2016, 2026, Oracle and/or its affiliates. All rights reserved.
# This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
import oci
import requests
# OCI Setup
oci_config = oci.config.from_file()
generative_ai_inference_client = oci.generative_ai_inference.GenerativeAiInferenceClient(
oci_config, service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com")
# NewsAPI Setup
# Create an API Key on NewsAPI, https://newsapi.org/.
# Replace 'YOUR_API_KEY' with the actual API key you obtained from NewsAPI
api_key = 'YOUR_API_KEY'
def create_media_dataset(api_key):
# We will collect the latest technology news.
news_dataset = str("Here is the latest technology news. ")
response = requests.get(f'https://newsapi.org/v2/top-headlines?country=us&category=technology&apiKey={api_key}')
if response.status_code == 200:
articles = response.json()['articles']
for article in articles:
if article['description']:
news_dataset = news_dataset + article['description'] + '. '
print("\n=== Today's Tech News ===\n", news_dataset)
return news_dataset
elif response.status_code == 401:
print("Unable to fetch news data. Make sure your API Key is set within the script.")
quit(-1)
else:
print(f"Unresolved Error: {response.status_code}")
quit(-1)
def generative_ai_function(oci_config, generative_ai_inference_client, news_dataset):
# Gen AI API Summary Model will be invoked with News Data.
summarize_text_response = generative_ai_inference_client.summarize_text(
summarize_text_details=oci.generative_ai_inference.models.SummarizeTextDetails(
input=news_dataset,
serving_mode=oci.generative_ai_inference.models.OnDemandServingMode(model_id="cohere.command"),
compartment_id=oci_config['tenancy'],
is_echo=False,
temperature=1,
length="MEDIUM",
format="PARAGRAPH",
extractiveness="AUTO")).data
print("\n=== Generative AI Tech News Summary ===\n", summarize_text_response.summary)
news_dataset = create_media_dataset(api_key)
generative_ai_function(oci_config, generative_ai_inference_client, news_dataset)