
GraphRAG:基于PolarDB+通義千問api+LangChain的知識圖譜定制實踐
# pip install requests
import requests
import base64
def generate_image(access_token, prompt):
url = "https://api.getimg.ai/v1/stable-diffusion/text-to-image"
headers = {"Authorization": "Bearer {}".format(access_token)}
data = {
"model": "stable-diffusion-v1-5",
"prompt": prompt,
"negative_prompt": "Disfigured, cartoon, blurry",
"width": 512,
"height": 512,
"steps": 25,
"guidance": 7.5,
"seed": 42,
"scheduler": "dpmsolver++",
"output_format": "jpeg",
}
response = requests.post(url, headers=headers, data=data)
image_string = response.content
image_bytes = base64.decodebytes(image_string)
with open("AI_Image.jpeg", "wb") as f:
f.write(image_bytes)
if __name__ == "__main__":
api_key = "YOUR_API_KEY"
prompt = "a photo of a cat dressed as a pirate"
image_bytes = generate_image(api_key, prompt)
需要人工智能校對器來糾正文本或文檔中的語法和拼寫錯誤,然后,使用下面的 API,它為您提供免費的 API 訪問權限,并允許您使用強大的語法檢查人工智能技術來修復您的文本。
在這里獲取您的 API
# AI Proofreading
# pip install requests
import requests
def Proofreader(text):
api_key = "YOUR_API_KEY"
url = 'https://api.sapling.ai/api/v1/edits'
data = {
'key': api_key,
'text': text,
'session_id': 'Test Document UUID',
'advanced_edits': {
'advanced_edits': True,
},
}
response = requests.post(url, json=data)
resp_json = response.json()
edits = resp_json['edits']
print("Corrections: ", edits)
if __name__ == '__main__':
Proofreader("I are going to the store, She don't likes pizza")
借助 Google Cloud 的文本轉語音 AI 技術,您可以將文本轉換為逼真的聲音。您可以靈活地選擇各種選項,例如,語言、音調、人們的聲音等等。
最重要的是,Google 提供免費的 API 供您使用。
在這里獲取您的 API
# AI Text to Speech
# pip install google-cloud-texttospeech
# pip install playsound
import io
import os
from google.cloud import texttospeech
import playsound
# Set the path to your credentials JSON file
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "credentials.json"
def Text_to_Speech(text):
client = texttospeech.TextToSpeechClient()
# Set the language code and the voice name.
language_code = "en-US"
voice_name = "en-US-Wavenet-A"
# Create a request to synthesize speech.
r = texttospeech.types.SynthesizeSpeechRequest()
r.text = text
r.voice = texttospeech.types.VoiceSelectionParams(
language_code=language_code, name=voice_name)
# Set the audio encoding.
r.audio_encoding = texttospeech.types.AudioEncoding.MP3
# Get the response from the API.
response = client.synthesize_speech(r)
# Save the audio to a file.
with io.open("audio.mp3", "wb") as f:
f.write(response.audio_content)
# Play the audio.
playsound.playsound("audio.mp3", True)
if __name__ == "__main__":
text = input("Enter the text: ")
Text_to_Speech(text)
如果您正在尋找類似于 chatGPT 的聊天機器人,您可以使用 OpenAI API。我在下面提供了一些代碼,演示如何使用 GPT 3.5 在 Python 中輕松創建個性化聊天機器人。
# ChatGPT AI
# pip install openai
import os
import openai
def ask(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": prompt
}
],
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
print("Ans: ", response)
if __name__ == "__main__":
ask("Python or JavaScript?")
您是否需要將掃描文檔轉換為文本或從圖像或掃描 PDF 中提取文本?您可以使用以下 OCR AI 技術從任何類型的圖像中提取文本。
下面的 API 利用了 Google Cloud Vision AI 技術,該技術擅長檢測和分析圖像中的文本。
在這里獲取您的 API
# AI OCR
# pip install google-cloud-vision
from google.cloud import vision
from google.cloud.vision import types
import os
# Set the path to your credentials JSON file
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "credentials.json"
def OCR(img_path):
client = vision.ImageAnnotatorClient()
with open(img_path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
if texts:
return texts[0].description
else:
return "No text found in the image."
if __name__ == "__main__":
image_path = "photo.jpg"
print(OCR(image_path))
最后的想法
在自動化工作方面,人工智能的能力非常出色。我希望這篇文章能為您提供一些有用的信息。如果您覺得有幫助,請分享給您的朋友,也許能夠幫助到他。
最后,感謝您的閱讀,編程愉快!
本文章轉載微信公眾號@web前端開發