太强了,8 行代码,开发一个AI应用

大家好,

发现一个超强的Python库,创建大模型应用不能更简单

8行代码足以(有2行非必须)

import gradio as gr
import ai_gradio

gr.load(
    name='qwen:qwen1.5-14b-chat', 
    src=ai_gradio.registry,
    title='AI Chat',
    description='Chat with an AI model'
).launch()

它还有语音聊天、视频聊天、相机模式、浏览器自动化等功能,功能强悍、使用极其简单。

项目地址:https://github.com/AK391/ai-gradio

ai-gradio 是一个 Python 包,可帮助开发者轻松创建由各种 AI 提供商支持的机器学习应用。它基于 Gradio 构建,为多个 AI 模型和服务提供统一接口。。

重要亮点
  • 功能丰富:ai-gradio 提供多提供商支持,集成了 15 个以上的 AI 提供商,包括 OpenAI、Google Gemini、Anthropic 等。具有文本聊天、语音聊天、视频聊天、代码生成、多模态、代理团队、浏览器自动化等功能。

  • 安装便捷:可以通过 pip 安装 ai-gradio 的核心包和特定提供商支持的包。有多种安装选项,如安装单个提供商支持、安装所有提供商等。

  • 使用示例多样:提供了 API 密钥配置方法、快速入门示例和高级功能示例。包括创建不同类型的聊天界面、编码助手、多提供商界面、CrewAI 团队、浏览器自动化和 Swarms 集成等。

Core Language Models
ProviderModels
OpenAIgpt-4-turbo, gpt-4, gpt-3.5-turbo
Anthropicclaude-3-opus, claude-3-sonnet, claude-3-haiku
Geminigemini-pro, gemini-pro-vision, gemini-2.0-flash-exp
Groqllama-3.2-70b-chat, mixtral-8x7b-chat
Specialized Models
ProviderTypeModels
LumaAIGenerationdream-machine, photon-1
DeepSeekMulti-purposedeepseek-chat, deepseek-coder, deepseek-vision
CrewAIAgent TeamsSupport Team, Article Team
QwenLanguageqwen-turbo, qwen-plus, qwen-max
BrowserAutomationbrowser-use-agent
安装与使用

安装很简单,pip install ai-gradio是必须的,还需要安装额外的大模型支持包,比如我只安装了pip install 'ai-gradio[deepseek]'pip install 'ai-gradio[qwen]',想省点事也可以一把梭哈pip install 'ai-gradio[all]'

# Install core package
pip install ai-gradio

# Install with specific provider support
pip install 'ai-gradio[openai]'     # OpenAI support
pip install 'ai-gradio[gemini]'     # Google Gemini support
pip install 'ai-gradio[anthropic]'  # Anthropic Claude support
pip install 'ai-gradio[groq]'       # Groq support
pip install 'ai-gradio[crewai]'     # CrewAI support
pip install 'ai-gradio[lumaai]'     # LumaAI support
pip install 'ai-gradio[xai]'        # XAI/Grok support
pip install 'ai-gradio[cohere]'     # Cohere support
pip install 'ai-gradio[sambanova]'  # SambaNova support
pip install 'ai-gradio[hyperbolic]' # Hyperbolic support
pip install 'ai-gradio[deepseek]'   # DeepSeek support
pip install 'ai-gradio[smolagents]' # SmolagentsAI support
pip install 'ai-gradio[fireworks]'  # Fireworks support
pip install 'ai-gradio[together]'   # Together support
pip install 'ai-gradio[qwen]'       # Qwen support
pip install 'ai-gradio[browser]'    # Browser support
# Install all providers
pip install 'ai-gradio[all]'

简单拿通义千问举个例

模型列表:https://bailian.console.aliyun.com/?spm=a2c4g.11186623.0.0.6f94b0a8AKJSUG#/model-market

API获取:https://bailian.console.aliyun.com/#/home

在控制台的右上角选择 API-KEY,然后创建一个,用于通过API调用大模型。

copy 后备用

题外话,下面是通义千问api调用示例,用Python使用Qwen很方便

import os
from openai import OpenAI

client = OpenAI(
    # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"), # 如何获取API Key:https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    model="qwen-plus", # 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
    messages=[
        {'role': 'system', 'content': 'You are a helpful assistant.'},
        {'role': 'user', 'content': '你是谁?'}
        ]
)
print(completion.choices[0].message.content)

ai-gradio例子,需要提前设置好api key

import os

os.environ["DASHSCOPE_API_KEY"] = "sk-上面复制好的"

其实ai-gradio还有窗口模式,同上,仅需提前备好api,就可以一个页面切换文本、图像、代码三模式使用。

import gradio as gr
import ai_gradio

with gr.Blocks() as demo:
    with gr.Tab("Text"):
        gr.load('openai:gpt-4-turbo', src=ai_gradio.registry)
    with gr.Tab("Vision"):
        gr.load('deepseek:deepseek-vision', src=ai_gradio.registry)
    with gr.Tab("Code"):
        gr.load('deepseek:deepseek-coder', src=ai_gradio.registry)

demo.launch()

语音输入和相机模式我没有深入玩

简单试了一下

gr.load(
    name='openai:gpt-4-turbo',
    src=ai_gradio.registry,
    enable_voice=True,
    title='AI Voice Assistant'
).launch()

报错如下

HTTP Error Your request was:

POST /Accounts/None/Tokens.json

Twilio returned the following information:

Unable to create record: Authentication Error - No credentials provided

More information may be available here:

https://www.twilio.com/docs/errors/20003

找了原因,camera和voice都依赖browser-use

项目地址

https://github.com/browser-use/browser-use

我看官方示例,感觉蛮强的,有机会再试

import asyncio
import os

from langchain_ollama import ChatOllama

from browser_use import Agent


async def run_search():
 agent = Agent(
  task=(
   '1. Go to https://www.reddit.com/r/LocalLLaMA'
   "2. Search for 'browser use' in the search bar"
   '3. Click search'
   '4. Call done'
  ),
  llm=ChatOllama(
   # model='qwen2.5:32b-instruct-q4_K_M',
   # model='qwen2.5:14b',
   model='qwen2.5:latest',
   num_ctx=128000,
  ),
  max_actions_per_step=1,
  tool_call_in_content=False,
 )

 await agent.run()


if __name__ == '__main__':
 asyncio.run(run_search())

如果你也对Python感兴趣,想学习Python,可以试试我整理的这一份全套的Python学习资料,【点击这里】免费领取!

包括:Python激活码+安装包、Python
web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程。带你从零基础系统性的学好Python!

① Python所有方向的学习路线图,清楚各个方向要学什么东西
② 100多节Python课程视频,涵盖必备基础、爬虫和数据分析
③ 100多个Python实战案例,学习不再是只会理论
④ 华为出品独家Python漫画教程,手机也能学习

在这里插入图片描述

本文转自 https://mp.weixin.qq.com/s/-tNEGa0yfNovDMeb3CHYdg,如有侵权,请联系删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值