Spaces:
Sleeping
Sleeping
gradio
#8
by
Da-123
- opened
- README.md +1 -1
- agentic_implementation/gradio_ag.py +102 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -5,7 +5,7 @@ colorFrom: yellow
|
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.0.1
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
| 10 |
short_description: Answer any questions you have about the content of your mail
|
| 11 |
---
|
|
|
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.0.1
|
| 8 |
+
app_file: agentic_implementation/gradio_ag.py
|
| 9 |
pinned: false
|
| 10 |
short_description: Answer any questions you have about the content of your mail
|
| 11 |
---
|
agentic_implementation/gradio_ag.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from typing import Any, Dict, List, Tuple
|
| 4 |
+
|
| 5 |
+
from re_act import (
|
| 6 |
+
get_plan_from_llm,
|
| 7 |
+
think,
|
| 8 |
+
act,
|
| 9 |
+
store_name_email_mapping,
|
| 10 |
+
extract_sender_info,
|
| 11 |
+
client,
|
| 12 |
+
)
|
| 13 |
+
from logger import logger # Assumes logger is configured
|
| 14 |
+
from schemas import PlanStep
|
| 15 |
+
|
| 16 |
+
# Maintain persistent session results
|
| 17 |
+
session_results: Dict[str, Any] = {}
|
| 18 |
+
|
| 19 |
+
def respond(
|
| 20 |
+
message: str,
|
| 21 |
+
history: List[Tuple[str, str]],
|
| 22 |
+
system_message: str,
|
| 23 |
+
max_tokens: int,
|
| 24 |
+
temperature: float
|
| 25 |
+
) -> str:
|
| 26 |
+
logger.info("Gradio agent received message: %s", message)
|
| 27 |
+
full_response = ""
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# Step 1: Generate plan
|
| 31 |
+
plan = get_plan_from_llm(message)
|
| 32 |
+
logger.debug("Generated plan: %s", plan)
|
| 33 |
+
full_response += "π **Plan**:\n"
|
| 34 |
+
for step in plan.plan:
|
| 35 |
+
full_response += f"- {step.action}\n"
|
| 36 |
+
full_response += "\n"
|
| 37 |
+
|
| 38 |
+
results = {}
|
| 39 |
+
|
| 40 |
+
# Step 2: Execute steps
|
| 41 |
+
for step in plan.plan:
|
| 42 |
+
if step.action == "done":
|
| 43 |
+
full_response += "β
Plan complete.\n"
|
| 44 |
+
break
|
| 45 |
+
|
| 46 |
+
should_run, updated_step, user_prompt = think(step, results, message)
|
| 47 |
+
|
| 48 |
+
# Ask user for clarification if needed
|
| 49 |
+
if user_prompt:
|
| 50 |
+
full_response += f"β {user_prompt} (Please respond with an email)\n"
|
| 51 |
+
return full_response # wait for user
|
| 52 |
+
|
| 53 |
+
if not should_run:
|
| 54 |
+
full_response += f"βοΈ Skipping `{step.action}`\n"
|
| 55 |
+
continue
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
output = act(updated_step)
|
| 59 |
+
results[updated_step.action] = output
|
| 60 |
+
full_response += f"π§ Ran `{updated_step.action}` β {output}\n"
|
| 61 |
+
except Exception as e:
|
| 62 |
+
logger.error("Error running action '%s': %s", updated_step.action, e)
|
| 63 |
+
full_response += f"β Error running `{updated_step.action}`: {e}\n"
|
| 64 |
+
break
|
| 65 |
+
|
| 66 |
+
# Step 3: Summarize results
|
| 67 |
+
try:
|
| 68 |
+
summary_rsp = client.chat.completions.create(
|
| 69 |
+
model="gpt-4o-mini",
|
| 70 |
+
temperature=temperature,
|
| 71 |
+
max_tokens=max_tokens,
|
| 72 |
+
messages=[
|
| 73 |
+
{"role": "system", "content": "Summarize these results for the user in a friendly way."},
|
| 74 |
+
{"role": "assistant", "content": json.dumps(results)}
|
| 75 |
+
],
|
| 76 |
+
)
|
| 77 |
+
summary = summary_rsp.choices[0].message.content
|
| 78 |
+
full_response += "\nπ **Summary**:\n" + summary
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error("Summary generation failed: %s", e)
|
| 81 |
+
full_response += "\nβ Failed to generate summary."
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
logger.exception("Unhandled error in agent: %s", e)
|
| 85 |
+
full_response += f"\nβ Unexpected error: {e}"
|
| 86 |
+
|
| 87 |
+
return full_response
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
demo = gr.ChatInterface(
|
| 91 |
+
respond,
|
| 92 |
+
additional_inputs=[
|
| 93 |
+
gr.Textbox(label="System message", value="You are an email assistant agent."),
|
| 94 |
+
gr.Slider(label="Max tokens", minimum=64, maximum=2048, value=512, step=1),
|
| 95 |
+
gr.Slider(label="Temperature", minimum=0.0, maximum=1.5, value=0.7, step=0.1),
|
| 96 |
+
],
|
| 97 |
+
title="π¬ Email Agent",
|
| 98 |
+
description="Ask me anything related to your email tasks!"
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -7,3 +7,5 @@ python-dateutil
|
|
| 7 |
beautifulsoup4
|
| 8 |
python-dotenv
|
| 9 |
pydantic[email]
|
|
|
|
|
|
|
|
|
| 7 |
beautifulsoup4
|
| 8 |
python-dotenv
|
| 9 |
pydantic[email]
|
| 10 |
+
gradio
|
| 11 |
+
loguru
|