Spaces:
Sleeping
Sleeping
Delete functions_huggingface.py
Browse files- functions_huggingface.py +0 -109
functions_huggingface.py
DELETED
|
@@ -1,109 +0,0 @@
|
|
| 1 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 2 |
-
import sqlite3
|
| 3 |
-
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 5 |
-
from langchain_core.runnables import RunnablePassthrough
|
| 6 |
-
import re
|
| 7 |
-
import gradio as gr
|
| 8 |
-
|
| 9 |
-
# Load the Llama model and tokenizer
|
| 10 |
-
model_name = "meta-llama/Llama-3.3-70B-Instruct"
|
| 11 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
| 13 |
-
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 14 |
-
|
| 15 |
-
# Initialize database connection
|
| 16 |
-
db_path = "Spring_2025_courses.db"
|
| 17 |
-
connection = sqlite3.connect(db_path)
|
| 18 |
-
|
| 19 |
-
def get_schema():
|
| 20 |
-
"""Retrieve database schema"""
|
| 21 |
-
cursor = connection.cursor()
|
| 22 |
-
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
| 23 |
-
tables = cursor.fetchall()
|
| 24 |
-
schema = {}
|
| 25 |
-
for table_name in tables:
|
| 26 |
-
table_name = table_name[0]
|
| 27 |
-
cursor.execute(f"PRAGMA table_info({table_name});")
|
| 28 |
-
columns = cursor.fetchall()
|
| 29 |
-
schema[table_name] = [column[1] for column in columns]
|
| 30 |
-
return schema
|
| 31 |
-
|
| 32 |
-
def run_query(query):
|
| 33 |
-
"""Execute SQL query"""
|
| 34 |
-
cursor = connection.cursor()
|
| 35 |
-
cursor.execute(query)
|
| 36 |
-
return cursor.fetchall()
|
| 37 |
-
|
| 38 |
-
# Prompt templates
|
| 39 |
-
system_prompt = """
|
| 40 |
-
You are a SQLite expert. Given an input question, create one syntactically correct SQLite query to run. Generate only one query. No preamble.
|
| 41 |
-
|
| 42 |
-
Here is the relevant table information:
|
| 43 |
-
schema: {schema}
|
| 44 |
-
|
| 45 |
-
Tips:
|
| 46 |
-
- Use LIKE instead of = in the queries
|
| 47 |
-
|
| 48 |
-
Write only one SQLite query that would answer the user's question.
|
| 49 |
-
"""
|
| 50 |
-
|
| 51 |
-
human_prompt = """Based on the table schema below, write a SQL query that would answer the user's question:
|
| 52 |
-
{schema}
|
| 53 |
-
|
| 54 |
-
Question: {question}
|
| 55 |
-
SQL Query:"""
|
| 56 |
-
|
| 57 |
-
prompt = ChatPromptTemplate.from_messages([
|
| 58 |
-
("system", system_prompt),
|
| 59 |
-
("human", human_prompt),
|
| 60 |
-
])
|
| 61 |
-
|
| 62 |
-
# Build query generation chain
|
| 63 |
-
sql_generator = (
|
| 64 |
-
RunnablePassthrough.assign(schema=get_schema)
|
| 65 |
-
| prompt
|
| 66 |
-
| StrOutputParser()
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
def generate_sql(question):
|
| 70 |
-
"""Generate SQL query from question"""
|
| 71 |
-
schema = get_schema()
|
| 72 |
-
input_prompt = system_prompt.format(schema=schema, question=question)
|
| 73 |
-
response = generator(input_prompt, max_length=512, num_return_sequences=1)
|
| 74 |
-
return response[0]['generated_text']
|
| 75 |
-
|
| 76 |
-
def execute_safe_query(question):
|
| 77 |
-
"""Safely execute a natural language query"""
|
| 78 |
-
try:
|
| 79 |
-
# Generate SQL query
|
| 80 |
-
sql_query = generate_sql(question)
|
| 81 |
-
|
| 82 |
-
# Validate SQL query
|
| 83 |
-
if not sql_query.strip().lower().startswith("select"):
|
| 84 |
-
return {"error": "Only SELECT queries are allowed.", "query": sql_query, "result": None}
|
| 85 |
-
|
| 86 |
-
# Execute query
|
| 87 |
-
result = run_query(sql_query)
|
| 88 |
-
return {"error": None, "query": sql_query, "result": result}
|
| 89 |
-
|
| 90 |
-
except Exception as e:
|
| 91 |
-
return {"error": str(e), "query": None, "result": None}
|
| 92 |
-
|
| 93 |
-
# Deploy using Gradio
|
| 94 |
-
def query_interface(question):
|
| 95 |
-
response = execute_safe_query(question)
|
| 96 |
-
if response['error']:
|
| 97 |
-
return f"Error: {response['error']}\nGenerated Query: {response['query']}"
|
| 98 |
-
return f"Query: {response['query']}\nResult: {response['result']}"
|
| 99 |
-
|
| 100 |
-
iface = gr.Interface(
|
| 101 |
-
fn=query_interface,
|
| 102 |
-
inputs="text",
|
| 103 |
-
outputs="text",
|
| 104 |
-
title="SQLite Query Generator with Llama 3.3",
|
| 105 |
-
description="Ask a natural language question about the Spring 2025 courses database and get the SQL query and results.",
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
if __name__ == "__main__":
|
| 109 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|