apilux / app.py
prnvtripathi14's picture
update
a6a5465 verified
raw
history blame
2.35 kB
import logging
from transformers import pipeline
import gradio as gr
# Set up logging
logging.basicConfig(
filename="app.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
# Load the generative AI model
logging.info("Loading the Hugging Face model...")
try:
model = pipeline("text2text-generation", model="google/flan-t5-large") # Replace with your chosen model
logging.info("Model loaded successfully.")
except Exception as e:
logging.error(f"Error loading the model: {e}")
raise
# Function to generate test cases
def generate_test_cases(api_info):
logging.info(f"Generating test cases for API info: {api_info}")
try:
prompt = (
f"Generate API test cases for the following API:\n\n{api_info}\n\n"
f"Test cases should include:\n- Happy path\n- Negative tests\n- Edge cases"
)
result = model(prompt, max_length=512, num_return_sequences=1)
logging.info(f"Test cases generated successfully.")
return result[0]['generated_text']
except Exception as e:
logging.error(f"Error generating test cases: {e}")
return "An error occurred while generating test cases."
# Process input and generate output
def process_input(url, method, headers, payload):
try:
logging.info("Received user input.")
api_info = f"URL: {url}\nMethod: {method}\nHeaders: {headers}\nPayload: {payload}"
logging.debug(f"Formatted API info: {api_info}")
test_cases = generate_test_cases(api_info)
return test_cases
except Exception as e:
logging.error(f"Error processing input: {e}")
return "An error occurred. Please check the input format and try again."
# Define Gradio interface
interface = gr.Interface(
fn=process_input,
inputs=[
gr.Textbox(label="API URL"),
gr.Textbox(label="HTTP Method"),
gr.Textbox(label="Headers (JSON format)"),
gr.Textbox(label="Payload (JSON format)"),
],
outputs="text",
title="API Test Case Generator"
)
# Launch Gradio app
if __name__ == "__main__":
try:
logging.info("Starting the Gradio app...")
interface.launch()
logging.info("Gradio app launched successfully.")
except Exception as e:
logging.error(f"Error launching the Gradio app: {e}")