jbbove commited on
Commit
50bdf93
·
verified ·
1 Parent(s): 9d95f9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
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 LLM to extract a simple textual knowledge graph from the input text.
20
-
21
- Args:
22
- text: a paragraph of text
23
-
24
- Returns:
25
- A string listing entities and relationships.
26
  """
27
- prompt = f"""Extract the entities and their relationships from the following text in the form of a simple knowledge graph with triples.
 
 
28
 
29
- Text: "{text}"
 
 
 
30
 
31
- Output format:
32
- (Subject) -[Relation]-> (Object)
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
- outputs = response.json()
42
- return outputs[0]["generated_text"]
43
  except Exception as e:
44
- return f"Could not parse output: {e}"
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: