Spaces:
Running
Running
File size: 3,041 Bytes
b1e5b99 202ad69 b1e5b99 202ad69 b1e5b99 202ad69 b1e5b99 202ad69 b1e5b99 202ad69 b1e5b99 202ad69 b1e5b99 202ad69 b1e5b99 202ad69 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
"""
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/')
|