Spaces:
Running
Running
Commit
·
608d7cd
1
Parent(s):
1a3ed87
Update space
Browse files
app.py
CHANGED
@@ -1,62 +1,106 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
3 |
|
4 |
-
""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
""
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import ast
|
4 |
+
from graphviz import Digraph
|
5 |
|
6 |
+
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
|
|
|
|
|
|
|
7 |
|
8 |
+
def sampling(num_samples, num_associations):
|
9 |
+
outputs = ast.literal_eval(client.chat.completions.create(
|
10 |
+
messages=[
|
11 |
+
{"role": "system", "content": "generate one json object, no explanation or additional text, use the following structure:\n"
|
12 |
+
"words: []\n"
|
13 |
+
f"{num_samples} samples in a list"
|
14 |
+
},
|
15 |
+
{"role": "user",
|
16 |
+
"content": f"synthesize {num_samples} random but widespread words for semantic modeling"},
|
17 |
+
],
|
18 |
+
response_format={
|
19 |
+
"type": "json",
|
20 |
+
"value": {
|
21 |
+
"properties": {
|
22 |
+
"words": {"type": "array", "items": {"type": "string"}},
|
23 |
+
}
|
24 |
+
}
|
25 |
+
},
|
26 |
+
stream=False,
|
27 |
+
max_tokens=1024,
|
28 |
+
temperature=0.7,
|
29 |
+
top_p=0.1
|
30 |
+
).choices[0].get('message')['content'])
|
31 |
|
32 |
+
fields = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
for word in outputs['words']:
|
35 |
+
fields[word] = ast.literal_eval(client.chat.completions.create(
|
36 |
+
messages=[
|
37 |
+
{"role": "system", "content": 'generate one json object, no explanation or additional text, use the following structure:\n'
|
38 |
+
'associations: []'
|
39 |
+
},
|
40 |
+
{"role": "user",
|
41 |
+
"content": f"synthesize {num_associations} associations for the word {word}"},
|
42 |
+
],
|
43 |
+
response_format={
|
44 |
+
"type": "json",
|
45 |
+
"value": {
|
46 |
+
"properties": {
|
47 |
+
"associations": {"type": "array", "items": {"type": "string"}}
|
48 |
+
}
|
49 |
+
}
|
50 |
+
},
|
51 |
+
stream=False,
|
52 |
+
max_tokens=2000,
|
53 |
+
temperature=0.7,
|
54 |
+
top_p=0.1
|
55 |
+
).choices[0].get('message')['content'])
|
56 |
|
57 |
+
triplets = []
|
58 |
|
59 |
+
for cluster in fields:
|
60 |
+
for association in fields[cluster]['associations']:
|
61 |
+
triplets.append(ast.literal_eval(client.chat.completions.create(
|
62 |
+
messages=[
|
63 |
+
{"role": "system", "content": "generate one json object, no explanation or additional text, use the following structure:\n"
|
64 |
+
"properties: [subject, predicate, object]\n"
|
65 |
+
"use chain-of-thought for predictions"
|
66 |
+
},
|
67 |
+
{"role": "user",
|
68 |
+
"content": f"form triplet based on semantics: generate predicate between the word {cluster} (subject) and the word {association} (object); return list with [subject, predicate, object]"},
|
69 |
+
],
|
70 |
+
response_format={
|
71 |
+
"type": "json",
|
72 |
+
"value": {
|
73 |
+
"properties": {
|
74 |
+
"properties": {"type": "array", "items": {"type": "string"}}
|
75 |
+
}
|
76 |
+
}
|
77 |
+
},
|
78 |
+
stream=False,
|
79 |
+
max_tokens=128,
|
80 |
+
temperature=0.7,
|
81 |
+
top_p=0.1
|
82 |
+
).choices[0].get('message')['content']))
|
83 |
|
84 |
+
dot = Digraph(comment=f'SynthNet, {num_samples} samples, {num_associations} associations', graph_attr={'rankdir': 'LR'})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
for entry in triplets:
|
87 |
+
source, label, target = entry['properties']
|
88 |
+
dot.node(source, source)
|
89 |
+
dot.node(target, target)
|
90 |
+
dot.edge(source, target, label=label)
|
91 |
|
92 |
+
dot.render('synthnet', format='png')
|
93 |
+
return 'synthnet.png'
|
94 |
|
95 |
+
demo = r.Interface(
|
96 |
+
inputs=[
|
97 |
+
gr.Slider(minimum=1, maximum=256, label="Number of Samples"),
|
98 |
+
gr.Slider(minimum=1, maximum=256, label="Number of Associations to each Sample"),
|
99 |
+
],
|
100 |
+
fn=sampling,
|
101 |
+
outputs=gr.Image(type="filepath"),
|
102 |
+
title="SynthNet",
|
103 |
+
description="Select a number of samples and assiciations to each sample to generate a graph.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
)
|
105 |
|
106 |
|