Please provide the full code in Gradio. The app uses an open-source LLM and a rule-based given doc to parse user prompts, find the most similar question in the doc, and execute the code listed in the doc.
The scenario is that the user can upload a csv file and ask questions like, "How can I find my customers' characteristics?"
Then, our LLM extracts the most relevant rule from the documentation and returns the run code along with its own explanation.
Assume we've already created a rule-based document containing the question and the Python code.
Rule 1: If the question concerns customer segmentation, run the segment code. Rule 2: If the question involves prediction, run the pred code. Users can ask questions in the document, and the bot will respond with the most relevant rule. How do we extract relevant code from the document, return it, and execute it?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To create a Gradio app that can extract relevant code from a rule-based document and execute it, you can follow these steps:
Import the necessary libraries:
python
import gradio as gr
import pandas as pd
import numpy as np
Load the rule-based document:
python
rules = pd.read_csv('rules.csv')
Assuming your rule-based document is a CSV file named rules.csv.
Preprocess the input text:
python
def preprocess(text):
return text.lower() # Lowercase the text
This function will lowercase the input text, which can help with text comparison.
Create a function to extract the relevant code:
python
def extract_code(text, rules):
similarities = {}
for rule in rules:
similarity = gradio.similarity(text, rule['question'])
if similarity > 0.5: # Adjust the threshold as needed
similarities[rule['question']] = rule['code']
return similarities
This function takes in the input text and the rule-based document. It calculates the similarity between the input text and each question in the document using the gradio.similarity() function. If the similarity is above a certain threshold (set to 0.5 in this example), it stores the corresponding code in a dictionary.
Create a Gradio app:
python
app = gr.App(
title="Code Executor",
description="Upload a CSV file and ask a question to execute the relevant code."
)
Define the inputs:
python
upload = app.input(
type="file",
name="csv_file",
description="Upload a CSV file"
)
question = app.input(
type="text",
name="question",
description="Ask a question"
)
This function takes in the input parameters and uses the extract_code() function to extract the relevant code from the rule-based document. If there is any relevant code, it executes the code using the exec() function. If there's an error during execution, it displays an error message. If no relevant code is found, it displays a message indicating that.
Run the app:
python
if __name__ == '__main__':
app.run(logic)
This line runs the app with the logic function as the entry point.
Now, when you run the app and upload a CSV file, it will execute the relevant code based on the question you ask.
Note:
This is a basic example, and you may want to add more features, such as error handling, input validation, and debugging tools.
The exec() function can be risky if the input code is not properly sanitized. Make sure to validate and sanitize the input code before executing it.
You can also use other libraries, such as nltk or spaCy, for natural language processing tasks, such as tokenization, stemming, and lemmatization.