File size: 1,762 Bytes
c64e302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07f569f
c64e302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from PIL import Image
import gradio as gr

from minerva import Minerva
from formatter import AutoGenFormatter


title = "Minerva: AI Guardian for Scam Protection"
description = """
              Built with AutoGen 0.4.0 and OpenAI. </br> 
              Analysis might take up to 30s. </br>
              https://github.com/dcarpintero/minerva
              """
inputs = gr.components.Image()
outputs = [
    gr.components.Textbox(label="Analysis Result"),
    gr.HTML(label="Agentic Workflow (Streaming)")
]
examples = "samples"

model = Minerva()
formatter = AutoGenFormatter()

def to_html(texts):
    formatted_html = ''
    for text in texts:
        formatted_html += text.replace('\n', '<br>') + '<br>'
    return f'<pre>{formatted_html}</pre>'

async def predict(img):
    try:
        img = Image.fromarray(img)

        stream = await model.analyze(img)

        streams = []
        messages = []
        async for s in stream:
            msg = await formatter.to_output(s)
            streams.append(s)
            messages.append(msg)
            yield ["", to_html(messages)]
        
        if streams[-1]:
            prediction = streams[-1].messages[-1].content
        else:
            prediction = "No analysis available. Try again later."

        await model.reset()
        yield [prediction, to_html(messages)]

    except Exception as e:
        print(e)
        yield ["Error during analysis. Try again later.", ""]


with gr.Blocks() as demo:
    with gr.Tab("Minerva: AI Guardian for Scam Protection"):
        gr.Interface(
            fn=predict,
            inputs=inputs,
            outputs=outputs,
            examples=examples,
            description=description,
        ).queue(default_concurrency_limit=5)

demo.launch()