Spaces:
Running
Running
""" | |
30-01-2025 | |
Rashid Javed | |
A simple dictionary to get word meaning, synonyms, antonyms | |
""" | |
from huggingface_hub import InferenceClient | |
import os | |
import requests as re | |
import json | |
import streamlit as st | |
##### Start Func Def part | |
## Call selected LLM, get word def and return | |
def call_llm(lmodel: str, lword: str, sysmsg: str, ltoken: str) -> str: | |
#ltoken = os.getenv('HF_TOKEN') | |
usermsg = lword | |
messages = [ | |
{ "role": "system", "content": sysmsg }, | |
{ "role": "user", "content": usermsg } | |
] | |
client = InferenceClient( | |
model= lmodel, | |
token= ltoken | |
) | |
completion = client.chat.completions.create( | |
model= lmodel, | |
messages= messages, | |
temperature=0.5, | |
max_tokens=2048, | |
top_p=0.7, | |
stream=False | |
) | |
return(completion.choices[0].message.content) | |
## Call Dictionary API and get word def | |
def call_dict_api(lword: str) -> dict: | |
dict_url = 'https://api.dictionaryapi.dev/api/v2/entries/en/' | |
api_url = dict_url + lword | |
response = re.get(url=api_url) | |
rjson = response.json() | |
if type(rjson) == dict: | |
return rjson | |
elif type(rjson) == list: | |
return rjson[0] | |
else: | |
return dict() | |
##### End Func Def part | |
sysmsg = '''You are an English language expert. You are precise in your output. | |
User will ask you a word and in your response you will include the word meaning, synonyms, antonyms, and word usage examples. | |
Format your output in different sections to make it easy to understand.''' | |
lmodel = 'meta-llama/Llama-3.2-3B-Instruct' | |
ltoken = os.getenv('HF_TOKEN') | |
llm_out = '' | |
api_out = dict() | |
st.set_page_config(page_title='Simple Dictionary', page_icon=':blue_book:', layout="wide") | |
st.title('A simple dictionary') | |
lword = st.text_input(label='Enter word') | |
lmodel = st.selectbox('Choose Language Model:', | |
index=0, | |
options=['meta-llama/Llama-3.2-3B-Instruct', 'Qwen/Qwen2.5-1.5B-Instruct', 'microsoft/Phi-3.5-mini-instruct'] | |
) | |
sys_prmpt = st.text_area(label='Sytem Prompt:', value=sysmsg) | |
chk1 = st.checkbox(label='Use Language model to get word definition', value=True) | |
chk2 = st.checkbox(label='Use Dictionary API call to get word definition', value=True) | |
btn_submit = st.button(label='Submit') | |
##st.write(os.environ) | |
if btn_submit: | |
if chk1: | |
llm_out = call_llm(lmodel, lword, sys_prmpt, ltoken) | |
if chk2: | |
api_out = call_dict_api(lword) | |
col1, col2 = st.columns(2, border=True) | |
with col1: | |
st.subheader(':green[LLM Output]') | |
if chk1: | |
st.write(llm_out) | |
else: | |
st.write('LLM Call option not selected') | |
with col2: | |
st.subheader(':orange[API Call]') | |
if chk2: | |
st.write(api_out) | |
else: | |
st.write('API Call option not selected') | |
st.divider() | |
st.write('*:blue[Developed By: Rashid Javed]*') | |
st.write('Dictionary API Call courtesy of ') | |
st.page_link('https://dictionaryapi.dev/', label='https://dictionaryapi.dev/') | |