rjavedv
further updates
b1e5b99
raw
history blame
4.14 kB
"""
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 Def part
## Call selected LLM, get word def and return
def call_llm(lmodel: str, lword: str) -> str:
ltoken = os.getenv('HF_TOKEN')
sysmsg = 'You are an English language expert. you are precise in your output. Users 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 read'
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) -> str:
return 'helo'
##### end Def part
lmodel = 'meta-llama/Llama-3.2-3B-Instruct'
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']
)
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)
col1, col2 = st.columns(2, border=True)
## when setting hf token in terminal remember to set witout ''
ltoken = os.getenv('HF_TOKEN')
print(ltoken)
#st.write(ltoken)
#lmodel = 'cardiffnlp/twitter-roberta-base-sentiment-latest' #Text classification
# lmodel = 'meta-llama/Llama-3.2-3B-Instruct' #Text Generation
# lmodel = 'microsoft/phi-4' #Not Working for inference API, model too large
# lmodel = 'PowerInfer/SmallThinker-3B-Preview' #Time out for this shit
lmodel = 'Qwen/Qwen2.5-1.5B-Instruct' #Working But output is in a strange format
# lmodel = 'mistralai/Mistral-Small-24B-Instruct-2501' #Not Working
# lmodel = 'microsoft/Phi-3.5-mini-instruct' #working
# client = InferenceClient(
# model= lmodel,
# token= ltoken
# )
# #loutput = client.text_classification("Today is a great day")
# #print('output=',loutput)
# completion = client.chat.completions.create(
# model= lmodel,
# messages= messages,
# temperature=0.5,
# max_tokens=2048,
# top_p=0.7
# #stream=False
# )
# print('Chat output : ')
# print(completion.choices[0].message.content)
sample_out = '''
Tolerate
Definition: To accept or allow something that one does not like or approve of, especially in a mild or passive way.
Synonyms: Accept, endure, put up with, overlook, tolerate
Antonyms: Disapprove, reject, condemn, despise
Usage examples:
1. The new law was difficult to tolerate.
2. I have to tolerate my boss's constant interruptions.
3. He was tolerant of her eccentricities.
4. We had to tolerate the long wait at the airport.
5. She tolerated his frequent late arrivals.
6. We have to tolerate the noise from the construction site.
7. He tolerated the criticism as a necessary part of his job.
8. She tolerated the small talk at the meeting.
9. We have to tolerate the inconvenience of the traffic.
10. He tolerated the long hours at work.
In these examples, "tolerate" is used to describe the acceptance or allowance of something that is not liked or approved of, often in a mild or passive manner.
'''
#### Dict API call
# dict_url = 'https://api.dictionaryapi.dev/api/v2/entries/en/'
# lword = 'Tolerate'
# api_url = dict_url + lword
# print(api_url)
# response = re.get(url=api_url)
# print(response.status_code)
# rjson = response.json()
# print(rjson)
# st.write(rjson)