Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from diffusers import AutoPipelineForText2Image
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# ---------- TEXT MODEL ----------
|
| 8 |
+
text_model = pipeline(
|
| 9 |
+
"text2text-generation",
|
| 10 |
+
model="google/flan-t5-base"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# ---------- IMAGE MODEL (FAST) ----------
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
|
| 16 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 17 |
+
"stabilityai/sdxl-turbo",
|
| 18 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 19 |
+
)
|
| 20 |
+
pipe = pipe.to(device)
|
| 21 |
+
|
| 22 |
+
# ---------- DECISION MODEL ----------
|
| 23 |
+
def decide_action(user_input):
|
| 24 |
+
prompt = f"""
|
| 25 |
+
Classify the user request.
|
| 26 |
+
|
| 27 |
+
Rules:
|
| 28 |
+
- If user wants image β return JSON: {{"action": "image"}}
|
| 29 |
+
- Otherwise β return JSON: {{"action": "text"}}
|
| 30 |
+
|
| 31 |
+
User: {user_input}
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
response = text_model(prompt, max_length=50)[0]["generated_text"]
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
decision = json.loads(response)
|
| 38 |
+
return decision.get("action", "text")
|
| 39 |
+
except:
|
| 40 |
+
return "text"
|
| 41 |
+
|
| 42 |
+
# ---------- TOOLS ----------
|
| 43 |
+
def text_tool(prompt):
|
| 44 |
+
response = text_model(prompt, max_length=200)
|
| 45 |
+
return response[0]["generated_text"]
|
| 46 |
+
|
| 47 |
+
def image_tool(prompt):
|
| 48 |
+
image = pipe(prompt, num_inference_steps=2, guidance_scale=0.0).images[0]
|
| 49 |
+
return image
|
| 50 |
+
|
| 51 |
+
# ---------- AGENT ----------
|
| 52 |
+
def agent(user_input):
|
| 53 |
+
action = decide_action(user_input)
|
| 54 |
+
|
| 55 |
+
if action == "image":
|
| 56 |
+
img = image_tool(user_input)
|
| 57 |
+
return "πΌοΈ Image generated", img
|
| 58 |
+
else:
|
| 59 |
+
text = text_tool(user_input)
|
| 60 |
+
return text, None
|
| 61 |
+
|
| 62 |
+
# ---------- UI ----------
|
| 63 |
+
def chat(user_input, history):
|
| 64 |
+
text, image = agent(user_input)
|
| 65 |
+
history = history + [(user_input, text)]
|
| 66 |
+
return history, image
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown("# π€ Agentic AI (Fast Text + Image)")
|
| 70 |
+
|
| 71 |
+
chatbot = gr.Chatbot()
|
| 72 |
+
image_output = gr.Image()
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
inp = gr.Textbox(placeholder="Ask anything or generate image...")
|
| 76 |
+
btn = gr.Button("Send")
|
| 77 |
+
|
| 78 |
+
state = gr.State([])
|
| 79 |
+
|
| 80 |
+
btn.click(chat, [inp, state], [chatbot, image_output])
|
| 81 |
+
|
| 82 |
+
demo.launch()
|