Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from union.remote import UnionRemote
|
| 3 |
+
from flytekit.types.file import FlyteFile
|
| 4 |
+
|
| 5 |
+
remote = UnionRemote()
|
| 6 |
+
|
| 7 |
+
# Fetch recent executions
|
| 8 |
+
recent_executions = remote.recent_executions(limit=10)
|
| 9 |
+
executions = [
|
| 10 |
+
e for e in recent_executions if e.spec.launch_plan.name == "bert-fine-tune.bert_ft"
|
| 11 |
+
]
|
| 12 |
+
recent_ex_id = executions[0].id.name
|
| 13 |
+
execution = remote.fetch_execution(name=recent_ex_id)
|
| 14 |
+
|
| 15 |
+
ft_llm = execution.outputs["o0"].remote_source
|
| 16 |
+
model_cache = execution.outputs["o1"].remote_source
|
| 17 |
+
|
| 18 |
+
predict_task = remote.fetch_task(name="bert-fine-tune.predict_sentiment")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def execute_flyte_task(text):
|
| 22 |
+
inputs = {
|
| 23 |
+
"text": text,
|
| 24 |
+
"model_cache_dir": model_cache,
|
| 25 |
+
"model": FlyteFile(ft_llm)
|
| 26 |
+
}
|
| 27 |
+
execution = remote.execute(predict_task, inputs=inputs, wait=True)
|
| 28 |
+
|
| 29 |
+
# Extract the response
|
| 30 |
+
response = execution.outputs["o0"]
|
| 31 |
+
|
| 32 |
+
# Format the response
|
| 33 |
+
sentiment = response['label']
|
| 34 |
+
score = response['score']
|
| 35 |
+
|
| 36 |
+
# Determine color based on sentiment
|
| 37 |
+
color = "red" if sentiment == "NEGATIVE" else "green"
|
| 38 |
+
|
| 39 |
+
# Format the output HTML
|
| 40 |
+
output_html = f"""
|
| 41 |
+
<div style="text-align: center;">
|
| 42 |
+
<h2>Sentiment: <span style="color: {color};">{sentiment}</span></h2>
|
| 43 |
+
<p>Confidence Score: {score:.2f}</p>
|
| 44 |
+
</div>
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
return output_html
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Launch Gradio app
|
| 51 |
+
iface = gr.Interface(
|
| 52 |
+
fn=execute_flyte_task,
|
| 53 |
+
inputs=["text"],
|
| 54 |
+
outputs=gr.HTML(), # Change output to HTML for better formatting
|
| 55 |
+
live=False,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
iface.launch(debug=True)
|