File size: 2,136 Bytes
ec9ef8b e030ac0 f24bed6 e030ac0 f24bed6 32680f1 e030ac0 f24bed6 e030ac0 1f73097 e030ac0 23432db e030ac0 23432db e030ac0 23432db e030ac0 |
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 |
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
# Load the chatbot model
chatbot_model_name = "facebook/bart-large-mnli"
chatbot_tokenizer = AutoTokenizer.from_pretrained(chatbot_model_name)
chatbot_model = AutoModelForCausalLM.from_pretrained(chatbot_model_name)
# Load the SQL model
sql_model_name = "your_sql_model_name" # Replace with the name of the SQL model you want to use
sql_tokenizer = AutoTokenizer.from_pretrained(sql_model_name)
sql_model = AutoModelForCausalLM.from_pretrained(sql_model_name)
def chatbot_response(user_message):
# Generate chatbot response using the chatbot model
inputs = chatbot_tokenizer.encode("User: " + user_message, return_tensors="pt")
outputs = chatbot_model.generate(inputs, max_length=100, num_return_sequences=1)
response = chatbot_tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
def execute_sql(user_query):
# Execute SQL query using the SQL model
inputs = sql_tokenizer(user_query, return_tensors="pt")
outputs = sql_model.generate(inputs['input_ids'], attention_mask=inputs['attention_mask'], max_length=1000)
response = sql_tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
# Define the chatbot and SQL execution interfaces using Gradio
chatbot_interface = gr.Interface(
fn=chatbot_response,
inputs=gr.Textbox(prompt="You:"),
outputs=gr.Textbox(),
live=True,
capture_session=True,
title="Chatbot",
description="Type your message in the box above, and the chatbot will respond.",
)
sql_execution_interface = gr.Interface(
fn=execute_sql,
inputs=gr.Textbox(prompt="Enter your SQL query:"),
outputs=gr.Textbox(),
live=True,
capture_session=True,
title="SQL Execution",
description="Type your SQL query in the box above, and the chatbot will execute it.",
)
# Combine the chatbot and SQL execution interfaces
combined_interface = gr.Interface([chatbot_interface, sql_execution_interface], layout="horizontal")
# Launch the combined Gradio interface
if __name__ == "__main__":
combined_interface.launch()
|