Spaces:
Running
Running
Diego Carpintero
commited on
Commit
·
c64e302
1
Parent(s):
d8cb895
implement gradio app
Browse files- app.py +66 -0
- formatter.py +37 -0
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
from minerva import Minerva
|
| 5 |
+
from formatter import AutoGenFormatter
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
title = "Minerva: AI Guardian for Scam Protection"
|
| 9 |
+
description = """
|
| 10 |
+
Built with AutoGen 0.4.0 and OpenAI. </br>
|
| 11 |
+
Analysis might take up to 30s. </br>
|
| 12 |
+
https://github.com/dcarpintero/minerva
|
| 13 |
+
"""
|
| 14 |
+
inputs = gr.components.Image()
|
| 15 |
+
outputs = [
|
| 16 |
+
gr.components.Textbox(label="Analysis Result"),
|
| 17 |
+
gr.HTML(label="Agentic Workflow (Streaming)")
|
| 18 |
+
]
|
| 19 |
+
examples = "samples"
|
| 20 |
+
|
| 21 |
+
model = Minerva()
|
| 22 |
+
formatter = AutoGenFormatter()
|
| 23 |
+
|
| 24 |
+
def to_html(texts):
|
| 25 |
+
formatted_html = ''
|
| 26 |
+
for text in texts:
|
| 27 |
+
formatted_html += text.replace('\n', '<br>') + '<br>'
|
| 28 |
+
return f'<pre>{formatted_html}</pre>'
|
| 29 |
+
|
| 30 |
+
async def predict(img):
|
| 31 |
+
try:
|
| 32 |
+
img = Image.fromarray(img)
|
| 33 |
+
|
| 34 |
+
stream = await model.analyze(img)
|
| 35 |
+
|
| 36 |
+
streams = []
|
| 37 |
+
messages = []
|
| 38 |
+
async for s in stream:
|
| 39 |
+
msg = await formatter.to_output(s)
|
| 40 |
+
streams.append(s)
|
| 41 |
+
messages.append(msg)
|
| 42 |
+
yield ["", to_html(messages)]
|
| 43 |
+
|
| 44 |
+
if streams[-1]:
|
| 45 |
+
prediction = streams[-1].messages[-1].content
|
| 46 |
+
else:
|
| 47 |
+
prediction = "No analysis available. Try again later."
|
| 48 |
+
|
| 49 |
+
yield [prediction, to_html(messages)]
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(e)
|
| 53 |
+
yield ["Error during analysis. Try again later.", ""]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
with gr.Tab("Minerva: AI Guardian for Scam Protection"):
|
| 58 |
+
gr.Interface(
|
| 59 |
+
fn=predict,
|
| 60 |
+
inputs=inputs,
|
| 61 |
+
outputs=outputs,
|
| 62 |
+
examples=examples,
|
| 63 |
+
description=description,
|
| 64 |
+
).queue(default_concurrency_limit=5)
|
| 65 |
+
|
| 66 |
+
demo.launch()
|
formatter.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import AsyncGenerator, List, TypeVar
|
| 2 |
+
|
| 3 |
+
from autogen_agentchat.base import Response, TaskResult
|
| 4 |
+
from autogen_agentchat.messages import AgentMessage, MultiModalMessage
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class AutoGenFormatter:
|
| 8 |
+
def _message_to_str(self, message: AgentMessage) -> str:
|
| 9 |
+
if isinstance(message, MultiModalMessage):
|
| 10 |
+
result: List[str] = []
|
| 11 |
+
for c in message.content:
|
| 12 |
+
if isinstance(c, str):
|
| 13 |
+
result.append(c)
|
| 14 |
+
else:
|
| 15 |
+
result.append("<image>")
|
| 16 |
+
return "\n".join(result)
|
| 17 |
+
else:
|
| 18 |
+
return f"{message.content}"
|
| 19 |
+
|
| 20 |
+
async def to_output(self, message: AgentMessage) -> str:
|
| 21 |
+
try:
|
| 22 |
+
if isinstance(message, TaskResult):
|
| 23 |
+
output = (
|
| 24 |
+
f"{'-' * 10} Summary {'-' * 10}\n"
|
| 25 |
+
f"Number of messages: {len(message.messages)}\n"
|
| 26 |
+
f"Finish reason: {message.stop_reason}\n"
|
| 27 |
+
)
|
| 28 |
+
return output
|
| 29 |
+
|
| 30 |
+
if isinstance(message, Response):
|
| 31 |
+
output = f"{'-' * 10} {message.chat_message.source} {'-' * 10}\n{self._message_to_str(message.chat_message)}\n"
|
| 32 |
+
return output
|
| 33 |
+
else:
|
| 34 |
+
output = f"{'-' * 10} {message.source} {'-' * 10}\n{self._message_to_str(message)}\n"
|
| 35 |
+
return output
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return ""
|