Created
July 18, 2024 04:29
-
-
Save Daksh777/3f03bf467d41f6261200e2f87e4644d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CLI tools for summarizing text by using Ollama, using Qwen2 Model | |
import click | |
import requests | |
import os | |
OLLAMA_API_URL = "http://localhost:11434/api/generate" | |
def summarize_text(text): | |
prompt = f"Please summarize the following text:\n\n{text}\n\nSummary:" | |
payload = { | |
"model": "qwen2:0.5b", | |
"prompt": prompt, | |
"stream": False | |
} | |
response = requests.post(OLLAMA_API_URL, json=payload) | |
if response.status_code == 200: | |
return response.json()['response'].strip() | |
else: | |
return f"Error: Unable to generate summary. Status code: {response.status_code}" | |
@click.command() | |
@click.option('-t', '--text-file', type=click.Path(exists=True), help='Path to the text file to summarize') | |
@click.argument('text', nargs=-1, required=False) | |
def main(text_file, text): | |
"""Summarize text from a file or direct input using Ollama API with Qwen2 0.5B model.""" | |
if text_file: | |
with open(text_file, 'r') as file: | |
content = file.read() | |
summary = summarize_text(content) | |
click.echo(f"Summary of {os.path.basename(text_file)}:\n{summary}") | |
elif text: | |
input_text = ' '.join(text) | |
summary = summarize_text(input_text) | |
click.echo(f"Summary of text pasted:\n{summary}") | |
else: | |
click.echo("Error: Please provide either a text file or direct text input.") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment