File size: 3,687 Bytes
33080cc
 
 
2fe32bb
33080cc
 
 
3bfe553
 
 
 
 
 
 
 
 
 
 
 
 
33080cc
 
 
 
 
 
 
2fe32bb
 
 
060a333
 
 
2fe32bb
3bfe553
 
 
 
33080cc
 
 
 
 
2fe32bb
 
33080cc
 
 
2a28d9d
 
33080cc
 
 
 
 
 
 
 
 
 
2a28d9d
33080cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f8f44b
 
3bfe553
 
 
 
9f8f44b
 
 
33080cc
 
 
 
 
 
354f0a1
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
101
102
103
104
105
import numpy as np
import gradio as gr
from z_generate import ServerlessInference
from z_embedding import load_vector_store

# STATIC TEXT DISPLAY
TXT_APP_DESCRIPTION = '''

# Just another RAG with images ✍️ 🖼️

## What its not

- Advance RAG; idk why [HF's tutorial](https://huggingface.co/learn/cookbook/en/advanced_rag) says so.

## How this works

1. First user query is passed to LLM 
2. LLM generated Response is matched against images's descriptions 
3. You see wonders. 

'''
TXT_SOURCE_DOC_DESCRIPTION = '''
Manually Downloaded as HTML files: 

1. https://en.wikipedia.org/wiki/MS_Dhoni
2. https://en.wikipedia.org/wiki/Jharkhand
2. https://en.wikipedia.org/wiki/Cricket_World_Cup

## Details

1. Vector Store is built using FAISS prior to starting this app. Although the vector store size in KBs but
    - the creation and loading of the store takes processing takes ~10GB RAM and lasts 5 mins. Hence **NOT BUILDING IT DURING RUNTIME OF APP**. 
    - `multi_process=True` was not working with HF Space `free-tier`.

2. Currently only html of wikipedia is supported as I am not wasting resources embedding tags of HTML, instead just parsing specific locations

3. Instead of Image tag, I have parsed figure/figcaption tags; much easy and better to find. Long run you can improve that part

'''


# UI Interface
demo = gr.Blocks()
vector_text, vector_image = load_vector_store()
llm = ServerlessInference(vector_store_text=vector_text, vector_store_images=vector_image)

# Processing Functions
def update_response(query:str = "something"):
    response_text, response_images = llm.perform_rag(query)
    return response_text, response_images
    
def update_gallery(text:str = "hell"):
    imgs = [
                ("http://www.marketingtool.online/en/face-generator/img/faces/avatar-1151ce9f4b2043de0d2e3b7826127998.jpg", "Some Description"),
                ("http://www.marketingtool.online/en/face-generator/img/faces/avatar-116b5e92936b766b7fdfc242649337f7.jpg", "Another Description")
            ]
    return imgs


def ask_bot(text):
    return update_response(text)

# UI Layout
with demo:
    gr.Markdown(TXT_APP_DESCRIPTION)

    with gr.Tabs():
        with gr.TabItem("Ask Bot"):
            with gr.Row(equal_height=True):
                with gr.Column(scale=3):
                    text_input = gr.Textbox(
                        label="You query here",
                        placeholder="What positions apart from crickter did Dhoni held?"
                    )
                with gr.Column(scale=1):
                    btn_generate = gr.Button("Generate Answer")

            with gr.Row():
                with gr.Column(scale=3):
                    text_output = gr.Textbox(label="Bot Response:", placeholder="Type in Query before I could answer")
                
                with gr.Column(scale=2): 
                    gallery = gr.Gallery(
                        label="Generated images", show_label=False, elem_id="gallery"
                        , columns=[3], rows=[1], object_fit="contain", height="auto"
                    )

            btn_generate.click(ask_bot, text_input, outputs=[text_output, gallery])

            examples = gr.Examples(
                examples=[
                    ["What are the achievements of Dhoni"],
                    ["In which state of India Dhoni was born ?"],
                    ["When did Dhoni won World Cup ?"],
                    ["Tell me something about Cricket world Cup of 2011"]
                ],
                inputs=[text_input],
            )


        ####
        with gr.TabItem("Source Documents"):
            gr.Markdown(TXT_SOURCE_DOC_DESCRIPTION)

demo.launch()