Spaces:
Sleeping
Sleeping
Oktay
commited on
initial commit
Browse files- app.py +110 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from textwrap import dedent
|
| 5 |
+
from agno.agent import Agent
|
| 6 |
+
from agno.team.team import Team
|
| 7 |
+
from agno.media import Image
|
| 8 |
+
from agno.models.anthropic import Claude
|
| 9 |
+
from agno.tools.googlesearch import GoogleSearchTools
|
| 10 |
+
|
| 11 |
+
# Load API key
|
| 12 |
+
load_dotenv()
|
| 13 |
+
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
|
| 14 |
+
if not ANTHROPIC_API_KEY:
|
| 15 |
+
raise ValueError("ANTHROPIC_API_KEY not found in .env file.")
|
| 16 |
+
|
| 17 |
+
# Define Agents
|
| 18 |
+
image_agent = Agent(
|
| 19 |
+
name="Image Analyzer",
|
| 20 |
+
role="Analyze uploaded car issue images to extract any visible mechanical problems or symptoms.",
|
| 21 |
+
model=Claude(id="claude-3-5-sonnet-20240620", api_key=ANTHROPIC_API_KEY)
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
info_agent = Agent(
|
| 25 |
+
name="Info Gatherer",
|
| 26 |
+
role="Use online search to collect information about known issues for the given car brand, model, and year.",
|
| 27 |
+
model=Claude(id="claude-3-haiku-20240307", api_key=ANTHROPIC_API_KEY),
|
| 28 |
+
tools=[GoogleSearchTools()],
|
| 29 |
+
show_tool_calls=False
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
agent_team = Team(
|
| 33 |
+
name="Smart Repair Team",
|
| 34 |
+
mode="coordinate",
|
| 35 |
+
model=Claude(id="claude-3-haiku-20240307", api_key=ANTHROPIC_API_KEY),
|
| 36 |
+
members=[image_agent, info_agent],
|
| 37 |
+
description="You are a task router that routes tasks to a team of agents to help users diagnose and fix car issues.",
|
| 38 |
+
instructions=dedent("""\
|
| 39 |
+
- First, use the uploaded image to detect possible issues visually.
|
| 40 |
+
- Then, use search tools to gather brand/model-specific info if available from the image extracted information.
|
| 41 |
+
- Give the user a helpful, step-by-step repair guide.
|
| 42 |
+
- Explain steps clearly for non-experts.
|
| 43 |
+
- Mention safety precautions and signs when to visit a professional.
|
| 44 |
+
"""),
|
| 45 |
+
expected_output=dedent("""\
|
| 46 |
+
Final response should be structured with these sections:
|
| 47 |
+
- Diagnosis Summary
|
| 48 |
+
- Step-by-Step Repair Instructions
|
| 49 |
+
"""),
|
| 50 |
+
success_criteria="The user receives a clear, accurate step-by-step guide for handling the car issue.",
|
| 51 |
+
add_datetime_to_instructions=True,
|
| 52 |
+
show_tool_calls=False,
|
| 53 |
+
enable_agentic_context=True,
|
| 54 |
+
show_members_responses=False,
|
| 55 |
+
markdown=True
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Main chat function
|
| 59 |
+
def diagnose_chat(image_pil, brand, model, year):
|
| 60 |
+
if image_pil is None:
|
| 61 |
+
return "Please upload an image of the car issue to proceed."
|
| 62 |
+
|
| 63 |
+
os.makedirs("temp_images", exist_ok=True)
|
| 64 |
+
temp_path = os.path.join("temp_images", "temp_car_issue.jpg")
|
| 65 |
+
image_pil.save(temp_path)
|
| 66 |
+
|
| 67 |
+
image_media = Image(filepath=temp_path)
|
| 68 |
+
|
| 69 |
+
# Construct message
|
| 70 |
+
extra_info = []
|
| 71 |
+
if brand:
|
| 72 |
+
extra_info.append(f"Brand: {brand}")
|
| 73 |
+
if model:
|
| 74 |
+
extra_info.append(f"Model: {model}")
|
| 75 |
+
if year:
|
| 76 |
+
extra_info.append(f"Year: {year}")
|
| 77 |
+
|
| 78 |
+
info_text = "\n".join(extra_info)
|
| 79 |
+
message = "Diagnose the car issue based on the uploaded image."
|
| 80 |
+
if info_text:
|
| 81 |
+
message += f"\n\nAdditional details:\n{info_text}"
|
| 82 |
+
|
| 83 |
+
response = agent_team.run(message=message, images=[image_media])
|
| 84 |
+
return response.content
|
| 85 |
+
|
| 86 |
+
# Gradio UI setup
|
| 87 |
+
with gr.Blocks() as demo:
|
| 88 |
+
gr.Markdown("# 🔧 SmartCarDoc\nUpload a car issue image to quickly get a repair plan.")
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
with gr.Column():
|
| 92 |
+
image_input = gr.Image(type="pil", label="Upload Image of Car Issue")
|
| 93 |
+
|
| 94 |
+
brand_input = gr.Textbox(label="Car Brand (optional)")
|
| 95 |
+
model_input = gr.Textbox(label="Car Model (optional)")
|
| 96 |
+
year_input = gr.Textbox(label="Year (optional)")
|
| 97 |
+
|
| 98 |
+
submit_button = gr.Button("Diagnose")
|
| 99 |
+
|
| 100 |
+
with gr.Column():
|
| 101 |
+
response_text = gr.Textbox(label="Repair Plan", lines=30, interactive=False)
|
| 102 |
+
|
| 103 |
+
submit_button.click(
|
| 104 |
+
fn=diagnose_chat,
|
| 105 |
+
inputs=[image_input, brand_input, model_input, year_input],
|
| 106 |
+
outputs=response_text
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
python-dotenv
|
| 3 |
+
agno
|
| 4 |
+
googlesearch-python
|
| 5 |
+
pycountry
|