Spaces:
Runtime error
Runtime error
File size: 1,988 Bytes
fb56148 13a2ed6 fb56148 4c0b5f0 13a2ed6 fb56148 9061bd6 a68ab69 9061bd6 13a2ed6 9e26908 fb56148 0690170 fb56148 13a2ed6 fb56148 0690170 13a2ed6 fb56148 | 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 | import os
import openai
from io import StringIO
import sys
import gradio as gr
from langchain.agents import create_csv_agent
from langchain.chat_models import AzureChatOpenAI
from langchain.chat_models import ChatOpenAI
from langchain.llms import AzureOpenAI
# os.environ["OPENAI_API_TYPE"] = openai.api_type = "azure"
# os.environ["OPENAI_API_BASE"] = openai.api_base = "https://aoai-southcentral.openai.azure.com/"
os.environ["OPENAI_API_KEY"] = openai.api_key = "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"
# openai.api_version = "2023-03-15-preview"
class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout
def answer_question(input_file, question):
llm = ChatOpenAI(temperature=0)
agent = create_csv_agent(llm, input_file.name, verbose=True)
question = question
with Capturing() as printed_text:
try:
answer = agent.run(question)
except Exception as e:
answer = "LLM: " + str(e)
import re
text = '\n'.join(printed_text) + '\n' + str(answer)
# Remove all escape characters
text = re.sub(r"\x1b\[\d+(;\d+)?m", "", text)
# Remove all characters inside angle brackets
text = re.sub(r"<.*?>", "", text)
# Remove all leading/trailing whitespaces
text = text.strip()
return text
with gr.Blocks(css="footer {visibility: hidden}", title="CSV Copilot") as demo:
csv_file = gr.State([])
csv_file = gr.File(label="CSV File", accept=".csv")
question = gr.Textbox(label="Question")
ask_question = gr.Button(label="Ask Question")
text_box = gr.TextArea(label="Output", lines=10)
ask_question.click(answer_question, inputs=[csv_file, question], outputs=text_box)
demo.launch()
|