Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load a free Hugging Face model (small + free to run)
|
| 5 |
+
generator = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 6 |
+
|
| 7 |
+
# Agent function
|
| 8 |
+
def agentic_ai(user_input):
|
| 9 |
+
# Step 1: Analyze input
|
| 10 |
+
analysis_prompt = f"Analyze the intent of this input: {user_input}"
|
| 11 |
+
analysis = generator(analysis_prompt, max_length=50, do_sample=False)[0]['generated_text']
|
| 12 |
+
|
| 13 |
+
# Step 2: Decide what to do (simple rule-based agent)
|
| 14 |
+
if "summarize" in user_input.lower():
|
| 15 |
+
task_prompt = f"Summarize this text in 2 lines: {user_input}"
|
| 16 |
+
elif "question" in user_input.lower() or "?" in user_input:
|
| 17 |
+
task_prompt = f"Answer this question briefly: {user_input}"
|
| 18 |
+
else:
|
| 19 |
+
task_prompt = f"Generate a helpful response: {user_input}"
|
| 20 |
+
|
| 21 |
+
# Step 3: LLM Response
|
| 22 |
+
response = generator(task_prompt, max_length=80, do_sample=False)[0]['generated_text']
|
| 23 |
+
|
| 24 |
+
# Step 4: Return both analysis + final response
|
| 25 |
+
return f"🔎 Agent Analysis: {analysis}\n\n💡 Agent Response: {response}"
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Gradio UI
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=agentic_ai,
|
| 31 |
+
inputs=gr.Textbox(lines=3, placeholder="Type your text here..."),
|
| 32 |
+
outputs="text",
|
| 33 |
+
title="🤖 Mini Agentic LLM App",
|
| 34 |
+
description="Smallest free demo of an Agentic AI using NLP + LLM on Hugging Face & Gradio."
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
demo.launch()
|