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)

相关推荐
吃着火锅x唱着歌1 分钟前
Effective C++ 学习笔记 条款45 运用成员函数模板接受所有兼容类型
c++·笔记·学习
Mr数据杨3 分钟前
贷款违约预测实战案例 从 Kaggle 表格分类到金融风控建模
人工智能·数据分析·kaggle竞赛
学着改变2754 分钟前
2026电磁流量计选型全维度指南:励磁技术、衬里材质与工况适配深度解析
大数据·人工智能·科技·产品运营·能源·材质
AI的探索之旅4 分钟前
97 个 OpenCV 实例(十四):图像分割,GrabCut 抠图 + inpaint 去水印
人工智能·opencv·计算机视觉
Capricorn19885 分钟前
Ox Alpha 1M 上下文科研引用失真怎么排查?知芽 Notebook Skill 技术链路实操
人工智能·笔记·python·深度学习·知识图谱·论文笔记
常山云栈7 分钟前
如何理解python中的鸭子模(类)型?
开发语言·python
火星上的鱼8 分钟前
2025年中国餐饮行业发展分析报告
大数据·人工智能
weixin199701080168 分钟前
《alibaba.idle.isv.order.ship 接入实录:闲鱼订单发货回传的5个隐式约束》(附Python源码)
服务器·前端·python
新知图书9 分钟前
14.2 多模态试驾预约Agent的系统架构
人工智能·深度学习·ai agent·智能体
y = xⁿ11 分钟前
DeepSeek harness工具系统什么设计的?
开发语言·python