File size: 3,137 Bytes
fe6f15c
 
 
4f6ca96
0cad3cd
 
 
 
 
fe6f15c
4f6ca96
fe6f15c
4f6ca96
 
 
fe6f15c
 
 
 
 
 
0cad3cd
928e524
0cad3cd
6a5020f
 
 
 
 
 
 
 
 
 
 
 
84fc049
6a5020f
4f6ca96
0cad3cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f6ca96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gradio as gr
from inference import generate_rag_caption

def predict(image):
    """
    Wrapper for Gradio to generate a caption from an uploaded image.
    Input: PIL Image
    Output: Caption (str)
    """
    caption = generate_rag_caption(image)
    return caption

# Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload an Image"),
    outputs=gr.Textbox(label="Generated Caption"),
    title="RAG Image Captioning",
    description="Upload an image to generate a caption using a RAG-based pipeline with CLIP, T5, and SentenceTransformer."
)

if __name__ == "__main__":
    iface.launch(share=True,ssr_mode=False)
    
# FastAPI app
app = FastAPI()

# REST API endpoint for image captioning
@app.post("/caption-image")
async def caption_image(file: UploadFile = File(...)):
    contents = await file.read()
    image = Image.open(io.BytesIO(contents)).convert("RGB")
    caption = generate_rag_caption(image)
    return {"caption": caption}

# Mount Gradio at root "/"
app = gr.mount_gradio_app(app, iface, path="/")

# import gradio as gr
# from inference import generate_rag_caption
# from fastapi import FastAPI, File, UploadFile
# from PIL import Image
# import io

# # Function for Gradio
# def predict(image):
#     caption = generate_rag_caption(image)
#     return caption

# # Gradio interface
# iface = gr.Interface(
#     fn=predict,
#     inputs=gr.Image(type="pil", label="Upload an Image"),
#     outputs=gr.Textbox(label="Generated Caption"),
#     title="RAG Image Captioning",
#     description="Upload an image to generate a caption using a RAG-based pipeline with CLIP, T5, and SentenceTransformer."
# )

# # FastAPI app
# app = FastAPI()

# # REST API endpoint for image captioning
# @app.post("/caption-image")
# async def caption_image(file: UploadFile = File(...)):
#     contents = await file.read()
#     image = Image.open(io.BytesIO(contents)).convert("RGB")
#     caption = generate_rag_caption(image)
#     return {"caption": caption}

# # Mount Gradio at root "/"
# app = gr.mount_gradio_app(app, iface, path="/")

# import gradio as gr
# from inference import generate_rag_caption
# from fastapi import FastAPI, File, UploadFile
# from PIL import Image
# import io

# # Step 1: Create FastAPI app
# app = FastAPI()

# # Step 2: Add an API route that accepts image uploads
# @app.post("/caption-image")
# async def caption_image(file: UploadFile = File(...)):
#     contents = await file.read()
#     image = Image.open(io.BytesIO(contents)).convert("RGB")
#     caption = generate_rag_caption(image)
#     return {"caption": caption}

# # Step 3: Create the original Gradio interface
# gradio_interface = gr.Interface(
#     fn=generate_rag_caption,
#     inputs=gr.Image(type="pil", label="Upload an Image"),
#     outputs=gr.Textbox(label="Generated Caption"),
#     title="RAG Image Captioning",
#     description="Upload an image to generate a caption using a RAG-based pipeline with CLIP, T5, and SentenceTransformer."
# )

# # ✅ Step 4: Mount Gradio interface on root path `/`
# app = gr.mount_gradio_app(app, gradio_interface, path="/")