Spaces:
Sleeping
Sleeping
Commit ·
608d7cd
1
Parent(s): 1a3ed87
Update space
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import gradio as grfrom huggingface_hub import InferenceClient"""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"""client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")def respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,):messages = [{"role": "system", "content": system_message}]for val in history:if val[0]:messages.append({"role": "user", "content": val[0]})if val[1]:messages.append({"role": "assistant", "content": val[1]})messages.append({"role": "user", "content": message})response = ""for message in client.chat_completion(messages,max_tokens=max_tokens,stream=True,temperature=temperature,top_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="You are a friendly Chatbot.", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",),],)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import gradio as grfrom huggingface_hub import InferenceClientimport astfrom graphviz import Digraphclient = InferenceClient("Qwen/Qwen2.5-72B-Instruct")def sampling(num_samples, num_associations):outputs = ast.literal_eval(client.chat.completions.create(messages=[{"role": "system", "content": "generate one json object, no explanation or additional text, use the following structure:\n""words: []\n"f"{num_samples} samples in a list"},{"role": "user","content": f"synthesize {num_samples} random but widespread words for semantic modeling"},],response_format={"type": "json","value": {"properties": {"words": {"type": "array", "items": {"type": "string"}},}}},stream=False,max_tokens=1024,temperature=0.7,top_p=0.1).choices[0].get('message')['content'])fields = {}for word in outputs['words']:fields[word] = ast.literal_eval(client.chat.completions.create(messages=[{"role": "system", "content": 'generate one json object, no explanation or additional text, use the following structure:\n''associations: []'},{"role": "user","content": f"synthesize {num_associations} associations for the word {word}"},],response_format={"type": "json","value": {"properties": {"associations": {"type": "array", "items": {"type": "string"}}}}},stream=False,max_tokens=2000,temperature=0.7,top_p=0.1).choices[0].get('message')['content'])triplets = []for cluster in fields:for association in fields[cluster]['associations']:triplets.append(ast.literal_eval(client.chat.completions.create(messages=[{"role": "system", "content": "generate one json object, no explanation or additional text, use the following structure:\n""properties: [subject, predicate, object]\n""use chain-of-thought for predictions"},{"role": "user","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]"},],response_format={"type": "json","value": {"properties": {"properties": {"type": "array", "items": {"type": "string"}}}}},stream=False,max_tokens=128,temperature=0.7,top_p=0.1).choices[0].get('message')['content']))dot = Digraph(comment=f'SynthNet, {num_samples} samples, {num_associations} associations', graph_attr={'rankdir': 'LR'})for entry in triplets:source, label, target = entry['properties']dot.node(source, source)dot.node(target, target)dot.edge(source, target, label=label)dot.render('synthnet', format='png')return 'synthnet.png'demo = r.Interface(inputs=[gr.Slider(minimum=1, maximum=256, label="Number of Samples"),gr.Slider(minimum=1, maximum=256, label="Number of Associations to each Sample"),],fn=sampling,outputs=gr.Image(type="filepath"),title="SynthNet",description="Select a number of samples and assiciations to each sample to generate a graph.",)
app.py+91-47