Hugging Face | 个人使用笔记

一、网站介绍

模型和数据集都是开源

搜索模型是默认按照趋势排序的

二、模型具体页面

三、调用API小练习

模型网站:flux-RealismLora

1.点击View Code 获取参考代码

2.创建一个python文件复制进一个代码编辑器

注意:需要补充最后一行保存代码

3.需要获得个人的hugging face的TOKEN



headers = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}中的hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx替换成上面的值

4.保存,运行

得到生成的图片

可能存在Runtime Error的问题,也不是一定会遇到,跟网络状况有关

5.写一个gradio页面,跟这个flux接口对接

python 复制代码
# 大模型生成代码
import gradio as gr
import requests
import io
from PIL import Image

API_URL = "https://api-inference.huggingface.co/models/XLabs-AI/flux-RealismLora"
headers = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

def query(prompt):
    payload = {
        "inputs": prompt
    }
    response = requests.post(API_URL, headers=headers, json=payload, timeout=30, verify=False)
    if response.status_code == 200:
        image_bytes = response.content
        image = Image.open(io.BytesIO(image_bytes))
        return image
    else:
        return f"Error: {response.status_code} - {response.text}"

# Define the Gradio interface without enable_queue
iface = gr.Interface(
    fn=query,
    inputs="text",
    outputs="image",
    title="Image Generation with Flux Realism Lora",
    description="Enter a prompt to generate an image using the Flux Realism Lora model."
)

# Launch the Gradio app
iface.launch()

网页如图:

注意: 对于 Hugging Face API:

(1)图像生成模型通常返回图像的二进制数据:

python 复制代码
image_bytes = query({
    "inputs": "Astronaut riding a horse",
})
image = Image.open(io.BytesIO(image_bytes))  # 将字节转换为图像

(2)其他类型的模型(如文本生成、情感分析等)可能返回 JSON 格式的响应,其中包含模型输出的详细信息:

python 复制代码
response = requests.post(API_URL, headers=headers, json=payload)
json_response = response.json()  # 解析 JSON 响应

四、以后补充

个人学习笔记 来源:https://www.bilibili.com/list/watchlater?bvid=BV1Mr4MewEY5\&oid=113236728874981![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/d6619abc5e5c4ac7be07af3947eabb96.png)

相关推荐
天天向上杰几秒前
通义灵码AI程序员
人工智能·aigc·ai编程
阿噜噜小栈4 分钟前
Cursor 无限续杯
经验分享·笔记
sendnews11 分钟前
AI赋能教育,小猿搜题系列产品携手DeepSeek打造个性化学习新体验
人工智能
查理零世13 分钟前
【蓝桥杯集训·每日一题2025】 AcWing 6134. 哞叫时间II python
python·算法·蓝桥杯
紫雾凌寒23 分钟前
解锁机器学习核心算法|神经网络:AI 领域的 “超级引擎”
人工智能·python·神经网络·算法·机器学习·卷积神经网络
WBingJ33 分钟前
2月17日深度学习日记
人工智能
zhengyawen66634 分钟前
深度学习之图像分类(一)
人工智能·深度学习·分类
sun lover35 分钟前
conda简单命令
python·conda
莫莫莫i38 分钟前
拆解微软CEO纳德拉战略蓝图:AI、量子计算、游戏革命如何改写未来规则!
人工智能·微软·量子计算
C#Thread41 分钟前
机器视觉--图像的运算(加法)
图像处理·人工智能·计算机视觉