Visualization of Machine Learning and Data Utilizing Huggingface GenAI API in Python

GenAI for ML with Simple Prompt in Python for Cleaning, Processing and Visualization


For Instructions Follow the above Video then go For the Code Input:



import requests, json
import pandas as pd  

key=open('rhf_api_key.txt','r').read().strip()
def get_mcqs(topic):

	r=requests.post('https://router.huggingface.co/v1/chat/completions',
	headers={'Authorization': f'Bearer {key}'},

	json={
		'model': 'arcee-ai/Trinity-Large-Thinking:featherless-ai',
		'messages':[{'role':'user','content':f'5 MCQs about:{topic} with options such as A, B, C, D. Return Valid JSON array'} 
	]
	}
		)

	content=r.json()['choices'][0]['message']['content']

	return json.loads(content)

data=get_mcqs('Machine Learning Python')
# print(data)
print(pd.DataFrame(data).to_csv('data.csv',index=False))


Visualize ML MCQs & Generate Image Utilizing GenAI Data in Python


For Instructions Follow the above Video then go For the Code Input:



import pandas as pd, matplotlib.pyplot as plt, ast 

df=pd.read_csv('data.csv')
df['options']=df['options'].apply(lambda x: ast.literal_eval(x) if isinstance(x, str) else x)

fig,ax = plt.subplots(figsize=(8.5, 11)); ax.axis('off')
y=0.96

for i,(q, opts, ans) in enumerate(zip(df['question'], df['options'], df['answer']),1):
	ax.text(0.05,y,f"{i}.{q}", fontsize=10, weight='bold',wrap=True); y-=0.05
	for o in opts:
		c=o[0]==ans.strip()
		ax.text(0.08,(y:=y-0.036), f"{'*' if c else 'o'} {o}",
			fontsize=9, color='green' if c else 'black', wrap=True)

	y-=0.09
# plt.show()
plt.savefig('mcq.png',dpi=150,bbox_inches='tight')