File size: 2,269 Bytes
3659338
 
 
 
 
 
 
 
 
b8062cd
3659338
b8062cd
3659338
 
b8062cd
3659338
 
b8062cd
3659338
b8062cd
3659338
 
b8062cd
 
3659338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8062cd
3659338
ab73ef2
60f20fa
3659338
 
 
 
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
import gradio as gr
from huggingface_hub import InferenceClient

if gr.NO_RELOAD:
    client = InferenceClient()

system_message = {
    "role": "system",
    "content": """
You are an expert in understanding comma separate files or .csv which has records of bank statement with salary and expenses.
You will be given a question and a set of answers along with a confidence score between 0 and 1 for each answer.
You job is to turn this information from this .csv file into a short, coherent response.

For example:
Question: "In which category I spent the most ?", answer: {"answer": "Transportation", "confidence": 0.98}

You should respond with something like:
With a high degree of confidence, I can say Transportation is where you are spending the most money. 

Question: "How much did I earn in the last year?", answer: [{"answer": "154.08", "confidence": 0.75}, {"answer": "155", "confidence": 0.25}

You should respond with something like:
You have earned $154.08 in the last year. 

"""}

def chat_fn(multimodal_message):
    question = multimodal_message["text"]
    image = multimodal_message["files"][0]
    
    answer = client.document_question_answering(image=image, question=question, model="impira/layoutlm-document-qa")
    
    answer = [{"answer": a.answer, "confidence": a.score} for a in answer]
   
    user_message = {"role": "user", "content": f"Question: {question}, answer: {answer}"}
   
    message = ""
    for token in client.chat_completion(messages=[system_message, user_message],
                           max_tokens=100, 
                           stream=True,
                           model="HuggingFaceH4/zephyr-7b-beta"):
        if token.choices[0].finish_reason is not None:
           continue
        message += token.choices[0].delta.content
        yield message

with gr.Blocks() as demo:
    gr.Markdown("# 🔍 Xray with your recent transitions")
    response = gr.Textbox(lines=5, label="Response")
    chat = gr.MultimodalTextbox(file_types=["image"], interactive=True,
                                show_label=False, placeholder="Upload a document image by clicking '+' and ask a question about your records.")
    chat.submit(chat_fn, inputs=chat, outputs=response)

if __name__ == "__main__":
    demo.launch()