Spaces:
Runtime error
Runtime error
File size: 1,631 Bytes
8ee963e eddfc1a 8ee963e eddfc1a e649211 eddfc1a 8ee963e f35b6aa e649211 8ee963e 4015cd6 8ee963e e649211 4015cd6 e649211 8ee963e 4015cd6 f35b6aa 4015cd6 eddfc1a 4015cd6 8ee963e 4015cd6 e649211 8ee963e eddfc1a 8ee963e eddfc1a 8ee963e eddfc1a |
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 |
import gradio as gr
import os
from llama_index import GPTSimpleVectorIndex
from langchain.agents import ZeroShotAgent, AgentExecutor
from langchain.agents import Tool
from langchain import OpenAI, LLMChain
os.environ['OPENAI_API_KEY'] = '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'
index = GPTSimpleVectorIndex.load_from_disk('/mnt/index/comboindex.json')
def querying_db(query: str):
response = index.query(query)
return response
tools = [
Tool(
name="QueryingDB",
func=querying_db,
description="useful for when you need to answer questions from the database. The answer is given in bullet points.",
return_direct=True
)
]
prefix = """Give a detailed answer to the question"""
suffix = """Give answer in bullet points"""
Question: {input}
{agent_scratchpad}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input", "agent_scratchpad"]
)
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
tool_names = [tool.name for tool in tools]
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
def get_answer(query_string):
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
result = agent_executor.run(query_string)
return result
def qa_app(query):
return get_answer(query)
inputs = gr.inputs.Textbox(label="Enter your question:")
output = gr.outputs.Textbox(label="Answer:")
iface = gr.Interface(fn=qa_app, inputs=inputs, outputs=output, title="Endo AI : Endocrine answering app by Dr. Om J Lakhani")
iface.launch()
|