ZolBot / app.py
Goutham-Vignesh's picture
Update app.py
a3f39a3 verified
raw
history blame
14.7 kB
import os
from typing import Optional, Tuple
import gradio as gr
from langchain.chains import ConversationChain
from langchain.llms import OpenAI
from langchain import PromptTemplate, HuggingFaceHub, LLMChain
from langchain.chains.conversation.memory import ConversationBufferMemory
from threading import Lock
import os
def load_chain():
"""Logic for loading the chain you want to use should go here."""
# template = """Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing consice summary on a given context. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
# Asisitant can able to provide answer from the conversation had with the user earlier. It should have access to the previous context
# {chat_history}
# Human: {human_input}
# Chatbot:"""
# prompt = PromptTemplate(
# input_variables=["chat_history", "human_input"],
# template=template
# )
# memory = ConversationBufferMemory(memory_key="chat_history")
#llm=HuggingFaceHub(repo_id="Goutham-Vignesh/flan-t5-tuned-zolvit", model_kwargs={"temperature":1e-10})
llm=HuggingFaceHub(repo_id="google/flan-t5-large", model_kwargs={"temperature":1e-10,"max_length":512})
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferMemory())
return conversation
def set_openai_api_key(api_key: str):
"""Set the api key and return chain.
If no api_key, then None is returned.
"""
if api_key:
os.environ["HUGGINGFACEHUB_API_TOKEN"] = api_key
chain = load_chain()
os.environ["HUGGINGFACEHUB_API_TOKEN"] = ""
return chain
class ChatWrapper:
def __init__(self):
self.lock = Lock()
def __call__(
self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
):
"""Execute the chat functionality."""
self.lock.acquire()
try:
history = history or []
# If chain is None, that is because no API key was provided.
if chain is None:
history.append((inp, "Please paste API key to use T5 Agent"))
return history, history
# Run chain and append input.
output = chain.predict(input=inp)
history.append((inp, output))
except Exception as e:
history.append((inp, "Invalid API key, Please paste Valid API key to use T5 Agent"))
return history, history
finally:
self.lock.release()
return history, history
chat = ChatWrapper()
block = gr.Blocks(css=".gradio-container {background-color: lightblue}")
with block:
with gr.Row():
gr.Markdown("<h3><center> 🚀 T5 Conversational Agent 💡</center></h3>")
openai_api_key_textbox = gr.Textbox(
placeholder="Please paste API key to use T5 Bot Agent",
show_label=False,
lines=1,
type="password",
css="#warning {color: red}"
)
chatbot = gr.Chatbot()
with gr.Row():
message = gr.Textbox(
label="What's your question?",
placeholder="Type your Query With prompts. Some of the sample prompts combined with query has been provided below",
lines=1,
)
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
gr.Examples(
examples=[
"Summarize the following text: The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 meters (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.",
"Summarize : GST (Goods and Services Tax) registration is a mandatory requirement for businesses with an annual turnover exceeding a certain threshold, which varies by country. GST is a unified tax that replaced several indirect taxes in many countries, and it is charged on the supply of goods and services. GST registration requires businesses to obtain a unique GST identification number, which is used to track their GST compliance and to claim input tax credits. The registration process may vary depending on the country and the nature of the business, but typically involves submitting various documents and information to the relevant tax authority. Failure to obtain GST registration can result in penalties and fines.",
"""Summarize the given text: A magnitude 4.2 earthquake shook the San Francisco area Friday at 4:42 a.m. PT (7:42 a.m. ET), the U.S. Geological Survey reported. The quake left about 2,000 customers without power, said David Eisenhower, a spokesman for Pacific Gas and Light. Under the USGS classification, a magnitude 4.2 earthquake is considered "light," which it says usually causes minimal damage. "We had quite a spike in calls, mostly calls of inquiry, none of any injury, none of any damage that was reported," said Capt. Al Casciato of the San Francisco police. "It was fairly mild." Watch police describe concerned calls immediately after the quake » . The quake was centered about two miles east-northeast of Oakland, at a depth of 3.6 miles, the USGS said. Oakland is just east of San Francisco, across San Francisco Bay. An Oakland police dispatcher told CNN the quake set off alarms at people's homes. The shaking lasted about 50 seconds, said CNN meteorologist Chad Myers. According to the USGS, magnitude 4.2 quakes are felt indoors and may break dishes and windows and overturn unstable objects. Pendulum clocks may stop. E-mail to a friend """,
"Answer the question : the sum and substance of the allegations made in the said complaint are that A1, while carrying out the investigation into the case of murder, had adopted an unending systematic extortion routine making demands through Mr. Ravinder Chadha, Advocate, and by creating pressure receiving money through his associates that included A2 and A3. It was stated in the complaint that A1 had called the said complainant Abhinav Krishan Aggarwal and his father Gopal Krishan Aggarwal several times to the office of crime branch, interrogating them and taking their signatures on blank papers and at times detaining them unnecessarily. He alleged that on 06.12.2007 his father was detained by A1 in the crime branch office and when he had met on 07.12.2007, A1 had demanded a bribe of Rs.8,00,000/- asking it to be brought to his residence on the next morning. He further alleged that when he had arranged and taken the said amount of money to the residence of A1, as instructed, the son of A1 had asked him to meet A1 at City Hospital, Pusa Road, where brother-in-law of A1 was admitted at the time. He stated that he had accordingly met A1 in the City Hospital, Pusa Road, where the said amount of money was handed over in the presence of one more person. He further stated that, as instructed by A1, he had met him again on the same date (08.12.2007) in the crime branch office when he was taken to the room of Mr. Joy Tirkey, ACP where he was told that the arrest of Gopal Krishan Aggarwal had to be effected under pressure and though he (the complainant) was also to be arrested in the case he was being spared and further that his father would be released in 10-15 days. It was further alleged that A1 had asked him (the complainant) to engage Mr. Ravinder Chadha, Advocate assuring that the said counsel would do the needful and that the said advocate upon being met demanded Rs.2 lacs to be paid, which payment was to be made on the next date. He alleged that after this, the extortion became a routine, A1 exerting pressure and demanding money through the said advocate now and then. He stated that he had started secretly recording (in audio/video mode) the conversations and demands of the said advocate, and A1, since 31.12.2007, the video recordings collected indicative of acceptance of illegal gratification by one of the named public servants. According to his complaint, he was victim of extortion to the extent of Rs.26-27 lacs, out of which Rs.8 lacs had been taken directly by A1 on 08.12.2007, the rest having been realized through Mr. Ravinder Chadha Advocate, which according to his allegations, included Rs.4 lacs as the share of the Advocate and the balance of A1. He also stated that the video recordings collected by him confirmed the manipulation in the record of the investigation of the case of murder. The complainant also alleged that after his father had been released on bail on 20.06.2008 by the High Court, A1 had extended threats to have him implicated in some other case unless further amount of Rs.25 lacs was paid to him and further that upon refusal to accede to such demand, A1 had extended threats, subsequently MCOCA case having been got registered due to manipulation by A1. The complainant submitted with the complaint “un-edited DVD” of audio and video recordings of the conversations along with typed copy of transcript of the relevant portions prepared by the complainant himself, seeing necessary action. Q: how much money did A1 demanded?",
"Answer the question using given context : It is settled position of law that extra- judicial confession, if true and voluntary, it can be relied upon by the court to convict the accused for the commission of the crime alleged. Despite the inherent weakness of extra-judicial confession as an item of evidence, it cannot be ignored when shown that such confession was made before a person who has no reason to state falsely and to whom it is made in the circumstances which tend to support the statement. Relying upon an earlier judgment in Rao Shiv Bahadur Singh v. State of Vindhya Pradesh, AIR 1954 SC 322, this Court again in Maghar Singh v. State of Punjab, (1975) 4 SCC 234, held that the evidence in the form of extra- judicial confession made by the accused to witnesses cannot be always termed to be a tainted evidence. Corroboration of such evidence is required only by way of abundant caution. If the court believes the witness before whom the confession is made and is satisfied that the confession was true and voluntarily made, then the conviction can be founded on such evidence alone. In Narayan Singh v. State of M.P., (1985) 4 SCC 26, this Court cautioned that it is not open to the court trying the criminal case to start with a presumption that extra-judicial confession is always a weak type of evidence. It would depend on the nature of the circumstances, the time when the confession is made and the credibility of the witnesses who speak for such a confession. The retraction of extra-judicial confession which is a usual phenomenon in criminal cases would by itself not weaken the case of the prosecution based upon such a confession. In Kishore Chand v. State of H.P., (1991) 1 SCC 286, this Court held that an unambiguous extra-judicial confession possesses high probative value force as it emanates from the person who committed the crime and is admissible in evidence provided it is free from suspicion, and suggestion of any falsity. However, before relying on the alleged confession, the court has to be satisfied that it is voluntary and is not the result of inducement, threat or promise envisaged under Section 24 of the Evidence Act or was brought about in suspicious circumstances to circumvent Sections 25 and 26. The Court is required to look into the surrounding circumstances to find out as to whether such confession is not inspired by any improper or collateral consideration or circumvention of law suggesting that it may not be true. All relevant circumstances such as the person to whom the confession is made, the time and place of making it, the circumstances in which it was made have to be scrutinized. To the same effect is the judgment in Baldev Raj v. State of Haryana, AIR 1991 SC 37. After referring to the judgment in Piara Singh v. State of Punjab, (1977) 4 SCC 452 this Court in Madan Gopal Kakkad v. Naval Dubey(1992) 3 SCC 204 held that the extra- judicial confession which is not obtained by coercion, promise of favour or false hope and is plenary in character and voluntary in nature can be made the basis for conviction even without corroboration. Q:What is the settled position of law regarding the admissibility of extra-judicial confession as evidence in a criminal case?",
"Please answer the following question. What is the boiling point of water?",
"Q: Can Obama have a conversation with George Washington? Give the rationale before answering",
"Generate a question for the following text: The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 meters (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct."
],
inputs=message,
)
gr.HTML("Demo application of a T5 conversational agent 🧠")
state = gr.State()
agent_state = gr.State()
submit.click(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
message.submit(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
openai_api_key_textbox.change(
set_openai_api_key,
inputs=[openai_api_key_textbox],
outputs=[agent_state],
)
block.launch()