Abraham E. Tavarez
commited on
Commit
Β·
99e6f81
1
Parent(s):
a340f3e
llama agent created
Browse files- llama_agent.py +62 -0
llama_agent.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from llama_index.llms.anthropic import Anthropic
|
| 3 |
+
from llama_index.core.tools import FunctionTool
|
| 4 |
+
from llama_index.core.agent import ReActAgent
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# π Import from your existing Gradio app
|
| 8 |
+
from tools import compare_faces, compare_voices, scan_video
|
| 9 |
+
|
| 10 |
+
# π Wrap them as tools
|
| 11 |
+
face_tool = FunctionTool.from_defaults(
|
| 12 |
+
fn=compare_faces,
|
| 13 |
+
name="Compare_Faces",
|
| 14 |
+
description="Compare two images to check if the faces match."
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
voice_tool = FunctionTool.from_defaults(
|
| 18 |
+
fn=compare_voices,
|
| 19 |
+
name="Compare_Voices",
|
| 20 |
+
description="Compare two audio files to check if voices belong to the same person."
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
video_tool = FunctionTool.from_defaults(
|
| 24 |
+
fn=scan_video,
|
| 25 |
+
name="Scan_Video",
|
| 26 |
+
description="Analyze a video using a reference image and optionally a YouTube link to detect face swaps."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# π Anthropic LLM
|
| 30 |
+
llm = Anthropic(model="claude-3-opus-20240229")
|
| 31 |
+
|
| 32 |
+
# π Create agent
|
| 33 |
+
agent = ReActAgent.from_tools(
|
| 34 |
+
tools=[face_tool, voice_tool, video_tool],
|
| 35 |
+
llm=llm,
|
| 36 |
+
verbose=True,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# π Chat function
|
| 41 |
+
def chat_with_agent(user_input, img1=None, img2=None, audio1=None, audio2=None, video=None, ref_img=None):
|
| 42 |
+
context = "You are an AI assistant. Use the following tools ONLY:\n\n"
|
| 43 |
+
context += "- 'Compare Faces' to compare two face images (requires img1_path, img2_path).\n"
|
| 44 |
+
context += "- 'Compare Voices' to compare two audio samples (requires audio1, audio2).\n"
|
| 45 |
+
context += "- 'Scan Video' to scan a video for deepfakes (requires video_file, ref_img).\n\n"
|
| 46 |
+
context += "Be precise in tool naming. Do not invent tool names.\n\n"
|
| 47 |
+
context += "User prompt:\n" + user_input + "\n"
|
| 48 |
+
|
| 49 |
+
if img1 and img2:
|
| 50 |
+
context += f"\nUploaded face images:\n - Image 1: {img1}\n - Image 2: {img2}"
|
| 51 |
+
if audio1 and audio2:
|
| 52 |
+
context += f"\nUploaded voice samples:\n - Audio 1: {audio1}\n - Audio 2: {audio2}"
|
| 53 |
+
if video:
|
| 54 |
+
context += f"\nUploaded video file:\n - Video: {video}"
|
| 55 |
+
if ref_img:
|
| 56 |
+
context += f"\nUploaded reference image:\n - Reference Image: {ref_img}\n"
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
response = agent.chat(context)
|
| 60 |
+
return response.response
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return f"β Agent error: {str(e)}"
|