Habib U Rehman commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,23 @@ import gradio as gr
|
|
| 4 |
from groq import Groq
|
| 5 |
import re
|
| 6 |
|
| 7 |
-
# Initialize Groq
|
| 8 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
-
|
| 10 |
MODEL = "llama-3.3-70b-versatile"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class AutonomousAgent:
|
| 13 |
def __init__(self, goal):
|
| 14 |
self.goal = goal
|
|
@@ -29,7 +41,6 @@ class AutonomousAgent:
|
|
| 29 |
def plan_tasks(self):
|
| 30 |
prompt = f"""
|
| 31 |
Goal: {self.goal}
|
| 32 |
-
|
| 33 |
Create a step-by-step task plan to achieve this goal.
|
| 34 |
Use short, clear tasks.
|
| 35 |
"""
|
|
@@ -48,7 +59,7 @@ class AutonomousAgent:
|
|
| 48 |
"""
|
| 49 |
return self.think(prompt)
|
| 50 |
|
| 51 |
-
def run(self, max_steps=6):
|
| 52 |
tasks_text = self.plan_tasks()
|
| 53 |
tasks = [re.sub(r'^\d+\.\s*', '', t).strip() for t in tasks_text.split("\n") if t.strip()]
|
| 54 |
|
|
@@ -57,21 +68,34 @@ class AutonomousAgent:
|
|
| 57 |
result = self.execute_task(task)
|
| 58 |
self.memory.append({"task": task, "result": result})
|
| 59 |
self.completed_tasks.append(task)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
# Gradio
|
| 65 |
-
def
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
interface = gr.Interface(
|
| 70 |
-
fn=
|
| 71 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
outputs="text",
|
| 73 |
-
title="🤖
|
| 74 |
-
description="
|
| 75 |
)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
|
|
|
| 4 |
from groq import Groq
|
| 5 |
import re
|
| 6 |
|
| 7 |
+
# Initialize Groq
|
| 8 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
|
|
|
| 9 |
MODEL = "llama-3.3-70b-versatile"
|
| 10 |
|
| 11 |
+
# ------------------ Direct Answer ------------------
|
| 12 |
+
def answer_anything(question):
|
| 13 |
+
response = client.chat.completions.create(
|
| 14 |
+
model=MODEL,
|
| 15 |
+
messages=[
|
| 16 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 17 |
+
{"role": "user", "content": question}
|
| 18 |
+
],
|
| 19 |
+
temperature=0.5
|
| 20 |
+
)
|
| 21 |
+
return response.choices[0].message.content
|
| 22 |
+
|
| 23 |
+
# ------------------ Autonomous Agent ------------------
|
| 24 |
class AutonomousAgent:
|
| 25 |
def __init__(self, goal):
|
| 26 |
self.goal = goal
|
|
|
|
| 41 |
def plan_tasks(self):
|
| 42 |
prompt = f"""
|
| 43 |
Goal: {self.goal}
|
|
|
|
| 44 |
Create a step-by-step task plan to achieve this goal.
|
| 45 |
Use short, clear tasks.
|
| 46 |
"""
|
|
|
|
| 59 |
"""
|
| 60 |
return self.think(prompt)
|
| 61 |
|
| 62 |
+
def run(self, max_steps=6, show_steps=False):
|
| 63 |
tasks_text = self.plan_tasks()
|
| 64 |
tasks = [re.sub(r'^\d+\.\s*', '', t).strip() for t in tasks_text.split("\n") if t.strip()]
|
| 65 |
|
|
|
|
| 68 |
result = self.execute_task(task)
|
| 69 |
self.memory.append({"task": task, "result": result})
|
| 70 |
self.completed_tasks.append(task)
|
| 71 |
+
if show_steps:
|
| 72 |
+
results.append(f"STEP {i}: {task}\nRESULT: {result}\n{'-'*40}")
|
| 73 |
+
|
| 74 |
+
# Return either detailed steps or just the final result
|
| 75 |
+
if show_steps:
|
| 76 |
+
return "\n".join(results)
|
| 77 |
+
else:
|
| 78 |
+
return result # last task result only
|
| 79 |
|
| 80 |
+
# ------------------ Gradio Interface ------------------
|
| 81 |
+
def run(user_input, mode, max_steps, show_steps):
|
| 82 |
+
if mode == "Direct Answer":
|
| 83 |
+
return answer_anything(user_input)
|
| 84 |
+
elif mode == "Autonomous Agent":
|
| 85 |
+
agent = AutonomousAgent(user_input)
|
| 86 |
+
return agent.run(max_steps=max_steps, show_steps=show_steps)
|
| 87 |
|
| 88 |
interface = gr.Interface(
|
| 89 |
+
fn=run,
|
| 90 |
+
inputs=[
|
| 91 |
+
gr.Textbox(label="Enter your question or goal"),
|
| 92 |
+
gr.Radio(choices=["Direct Answer", "Autonomous Agent"], label="Mode"),
|
| 93 |
+
gr.Slider(1, 10, value=6, step=1, label="Max Steps (for Autonomous Agent)"),
|
| 94 |
+
gr.Checkbox(label="Show steps (for Autonomous Agent)", value=True)
|
| 95 |
+
],
|
| 96 |
outputs="text",
|
| 97 |
+
title="🤖 Combined AI Agent",
|
| 98 |
+
description="Choose 'Direct Answer' for quick responses, or 'Autonomous Agent' for multi-step goal execution."
|
| 99 |
)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|