Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,40 +8,50 @@ from tools.final_answer import FinalAnswerTool
|
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
-
API_URL = "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.2-11B-Vision-Instruct/v1/chat/completions"
|
| 12 |
-
headers = {
|
| 13 |
-
"Authorization": f"Bearer {os.environ['HF_TOKEN']}",
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
@tool
|
| 17 |
def extract_knowledge_graph(text: str) -> str:
|
| 18 |
"""
|
| 19 |
-
Uses a Hugging Face
|
| 20 |
-
|
| 21 |
-
Args:
|
| 22 |
-
text: a paragraph of text
|
| 23 |
-
|
| 24 |
-
Returns:
|
| 25 |
-
A string listing entities and relationships.
|
| 26 |
"""
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
| 36 |
-
|
| 37 |
if response.status_code != 200:
|
| 38 |
return f"API error: {response.status_code} - {response.text}"
|
| 39 |
-
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
return
|
| 43 |
except Exception as e:
|
| 44 |
-
return f"
|
| 45 |
|
| 46 |
@tool
|
| 47 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
def extract_knowledge_graph(text: str) -> str:
|
| 13 |
"""
|
| 14 |
+
Uses a Hugging Face chat model to extract entities and relations from the input text.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 17 |
+
if not HF_TOKEN:
|
| 18 |
+
return "Error: Hugging Face token not found."
|
| 19 |
|
| 20 |
+
headers = {
|
| 21 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 22 |
+
"Content-Type": "application/json"
|
| 23 |
+
}
|
| 24 |
|
| 25 |
+
API_URL = "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.2-11B-Vision-Instruct/v1/chat/completions"
|
| 26 |
+
|
| 27 |
+
# Format the prompt
|
| 28 |
+
prompt = f"""Extract all the entities and their relationships from this text as structured knowledge triples.
|
| 29 |
+
|
| 30 |
+
Text: "{text}"
|
| 31 |
+
|
| 32 |
+
Format:
|
| 33 |
+
(Subject) -[Relation]-> (Object)
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
payload = {
|
| 37 |
+
"messages": [
|
| 38 |
+
{"role": "system", "content": "You are a helpful assistant that extracts knowledge graphs from text."},
|
| 39 |
+
{"role": "user", "content": prompt}
|
| 40 |
+
],
|
| 41 |
+
"temperature": 0.5,
|
| 42 |
+
"max_tokens": 1024
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 46 |
|
|
|
|
|
|
|
| 47 |
if response.status_code != 200:
|
| 48 |
return f"API error: {response.status_code} - {response.text}"
|
| 49 |
+
|
| 50 |
try:
|
| 51 |
+
output = response.json()["choices"][0]["message"]["content"]
|
| 52 |
+
return output
|
| 53 |
except Exception as e:
|
| 54 |
+
return f"Failed to parse response: {e}"
|
| 55 |
|
| 56 |
@tool
|
| 57 |
def get_current_time_in_timezone(timezone: str) -> str:
|