Update README.md
Browse files
README.md
CHANGED
@@ -80,6 +80,69 @@ PARAMETER stop "<s>"
|
|
80 |
PARAMETER stop "</s>"
|
81 |
```
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
### 🗞️ Configuration
|
84 |
The YAML configuration for this model:
|
85 |
|
|
|
80 |
PARAMETER stop "</s>"
|
81 |
```
|
82 |
|
83 |
+
## 💻 Ollama Python Test Code
|
84 |
+
|
85 |
+
```
|
86 |
+
import sys
|
87 |
+
import os
|
88 |
+
import requests
|
89 |
+
from bs4 import BeautifulSoup
|
90 |
+
from langchain_community.chat_models import ChatOllama
|
91 |
+
from langchain.schema import AIMessage, HumanMessage, SystemMessage
|
92 |
+
|
93 |
+
def clean_output(text):
|
94 |
+
text = text.replace("</s>", "").strip()
|
95 |
+
return text
|
96 |
+
|
97 |
+
def invoke_model(text):
|
98 |
+
messages = [
|
99 |
+
SystemMessage(content='You are an expert copywriter with expertise in summarizing documents.'),
|
100 |
+
HumanMessage(content=f'Please provide a short and concise summary of the following text:\nTEXT: {text}')
|
101 |
+
]
|
102 |
+
|
103 |
+
try:
|
104 |
+
llm = ChatOllama(model="smartllama-3-Ko-8b-256k-pose:latest")
|
105 |
+
summary_output = llm.invoke(messages)
|
106 |
+
if isinstance(summary_output, AIMessage):
|
107 |
+
cleaned_content = clean_output(summary_output.content)
|
108 |
+
return cleaned_content
|
109 |
+
else:
|
110 |
+
return "Unexpected data type for model output."
|
111 |
+
except Exception as e:
|
112 |
+
print(f"An error occurred while processing the model output: {str(e)}")
|
113 |
+
return None
|
114 |
+
|
115 |
+
def fetch_text_from_url(url):
|
116 |
+
try:
|
117 |
+
response = requests.get(url)
|
118 |
+
response.raise_for_status() # Raises an HTTPError for bad responses
|
119 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
120 |
+
# Extract main content from paragraphs within the article body only
|
121 |
+
content = soup.find('div', {'id': 'bodyContent'})
|
122 |
+
paragraphs = content.find_all('p')
|
123 |
+
text_content = ' '.join(p.text for p in paragraphs)
|
124 |
+
return text_content
|
125 |
+
except requests.RequestException as e:
|
126 |
+
print(f"Failed to fetch data from URL: {str(e)}")
|
127 |
+
return None
|
128 |
+
|
129 |
+
def summarize_content(source):
|
130 |
+
text_content = fetch_text_from_url(source) if source.startswith(('http://', 'https://')) else read_text_file(source)
|
131 |
+
if text_content:
|
132 |
+
summary = invoke_model(text_content)
|
133 |
+
print("Summary of the document:")
|
134 |
+
print(summary)
|
135 |
+
else:
|
136 |
+
print("No text found or unable to extract text from source.")
|
137 |
+
|
138 |
+
if __name__ == '__main__':
|
139 |
+
if len(sys.argv) < 2:
|
140 |
+
print("Usage: python script.py <file_path_or_url>")
|
141 |
+
else:
|
142 |
+
source = sys.argv[1]
|
143 |
+
summarize_content(source)
|
144 |
+
```
|
145 |
+
|
146 |
### 🗞️ Configuration
|
147 |
The YAML configuration for this model:
|
148 |
|