Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import requests | |
| import torch | |
| from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM | |
| # Shopify API credentials | |
| ACCESS_TOKEN = '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' | |
| SHOP_NAME = '6znwwf-77.myshopify.com' | |
| # Use a pipeline as a high-level helper for SQL generation | |
| sql_generation_pipe = pipeline("text-generation", model="defog/sqlcoder-7b-2") | |
| def generate_sql(natural_language_query): | |
| """Convert natural language to SQL query using a pipeline.""" | |
| input_prompt = f"Convert the following query to SQL:\n{natural_language_query}" | |
| generated_sql = sql_generation_pipe(input_prompt, max_length=512)[0]['generated_text'] | |
| return generated_sql | |
| def fetch_data_from_shopify(endpoint, params): | |
| """Fetch data from Shopify API based on endpoint and parameters.""" | |
| headers = { | |
| 'X-Shopify-Access-Token': ACCESS_TOKEN, | |
| 'Content-Type': 'application/json' | |
| } | |
| url = f"https://{SHOP_NAME}/admin/api/2023-10/{endpoint}.json" | |
| response = requests.get(url, headers=headers, params=params) | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| return {"error": f"Error fetching data: {response.status_code} - {response.text}"} | |
| def sql_to_api_params(sql_query): | |
| """Convert SQL query into API parameters.""" | |
| if "SELECT" in sql_query and "FROM" in sql_query: | |
| parts = sql_query.split("WHERE") | |
| if len(parts) == 2: | |
| conditions = parts[1].strip() | |
| condition_parts = conditions.split("=") | |
| if len(condition_parts) == 2: | |
| param_name = condition_parts[0].strip().replace(" ", "").lower() | |
| param_value = condition_parts[1].strip().strip('"') | |
| params = {param_name: param_value} | |
| return params | |
| return {} | |
| # Streamlit application | |
| st.title("Shopify SQL Query Interface") | |
| user_input = st.text_input("Enter your query (e.g., 'Show me products of type Nilesh'):") | |
| if st.button("Submit"): | |
| if user_input: | |
| # Generate SQL query from natural language | |
| sql_query = generate_sql(user_input) | |
| st.write(f"Generated SQL Query: {sql_query}") | |
| # Convert SQL query to API parameters | |
| params = sql_to_api_params(sql_query) | |
| st.write(f"API Parameters: {params}") | |
| # Fetch data from Shopify | |
| if params: | |
| products = fetch_data_from_shopify("products", params) | |
| st.write("Results:") | |
| st.json(products) | |
| else: | |
| st.warning("Could not convert SQL query to API parameters.") | |
| else: | |
| st.warning("Please enter a query.") | |