Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Commit 
							
							·
						
						b653672
	
1
								Parent(s):
							
							5506729
								
adding dependancies
Browse files- app.py +21 -10
 - requirments.txt +3 -2
 
    	
        app.py
    CHANGED
    
    | 
         @@ -1,28 +1,37 @@ 
     | 
|
| 1 | 
         
             
            import gradio as gr
         
     | 
| 2 | 
         
            -
            from transformers import AutoTokenizer,  
     | 
| 
         | 
|
| 3 | 
         | 
| 4 | 
         
             
            # Load model + tokenizer
         
     | 
| 5 | 
         
            -
            model_name = " 
     | 
| 6 | 
         
             
            tokenizer = AutoTokenizer.from_pretrained(model_name)
         
     | 
| 7 | 
         
            -
            model =  
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 8 | 
         | 
| 9 | 
         
             
            def text_to_sql(question, schema=""):
         
     | 
| 10 | 
         
             
                """
         
     | 
| 11 | 
         
             
                Convert natural language question into SQL query.
         
     | 
| 12 | 
         
             
                Schema can be passed as a string (table + column names).
         
     | 
| 13 | 
         
             
                """
         
     | 
| 14 | 
         
            -
                # Format input as Spider expects
         
     | 
| 15 | 
         
             
                if schema:
         
     | 
| 16 | 
         
            -
                    prompt = f"{schema} {question}"
         
     | 
| 17 | 
         
             
                else:
         
     | 
| 18 | 
         
            -
                    prompt = question
         
     | 
| 19 | 
         | 
| 20 | 
         
            -
                inputs = tokenizer(prompt, return_tensors="pt" 
     | 
| 21 | 
         
            -
                outputs = model.generate( 
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 22 | 
         
             
                sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
         
     | 
| 23 | 
         
             
                return sql_query
         
     | 
| 24 | 
         | 
| 25 | 
         
            -
            # Define  
     | 
| 26 | 
         
             
            iface = gr.Interface(
         
     | 
| 27 | 
         
             
                fn=text_to_sql,
         
     | 
| 28 | 
         
             
                inputs=[
         
     | 
| 
         @@ -30,7 +39,9 @@ iface = gr.Interface( 
     | 
|
| 30 | 
         
             
                    gr.Textbox(label="Schema (optional)", placeholder="table: columns, ...")
         
     | 
| 31 | 
         
             
                ],
         
     | 
| 32 | 
         
             
                outputs="text",
         
     | 
| 
         | 
|
| 
         | 
|
| 33 | 
         
             
            )
         
     | 
| 34 | 
         | 
| 35 | 
         
            -
            # Launch  
     | 
| 36 | 
         
             
            iface.launch()
         
     | 
| 
         | 
|
| 1 | 
         
             
            import gradio as gr
         
     | 
| 2 | 
         
            +
            from transformers import AutoTokenizer, AutoModelForCausalLM
         
     | 
| 3 | 
         
            +
            import torch
         
     | 
| 4 | 
         | 
| 5 | 
         
             
            # Load model + tokenizer
         
     | 
| 6 | 
         
            +
            model_name = "premai-io/prem-1B-SQL"
         
     | 
| 7 | 
         
             
            tokenizer = AutoTokenizer.from_pretrained(model_name)
         
     | 
| 8 | 
         
            +
            model = AutoModelForCausalLM.from_pretrained(
         
     | 
| 9 | 
         
            +
                model_name,
         
     | 
| 10 | 
         
            +
                torch_dtype=torch.float16,
         
     | 
| 11 | 
         
            +
                device_map="auto"  # Uses GPU if available on Spaces
         
     | 
| 12 | 
         
            +
            )
         
     | 
| 13 | 
         | 
| 14 | 
         
             
            def text_to_sql(question, schema=""):
         
     | 
| 15 | 
         
             
                """
         
     | 
| 16 | 
         
             
                Convert natural language question into SQL query.
         
     | 
| 17 | 
         
             
                Schema can be passed as a string (table + column names).
         
     | 
| 18 | 
         
             
                """
         
     | 
| 
         | 
|
| 19 | 
         
             
                if schema:
         
     | 
| 20 | 
         
            +
                    prompt = f"{schema}\nQuestion: {question}\nSQL:"
         
     | 
| 21 | 
         
             
                else:
         
     | 
| 22 | 
         
            +
                    prompt = f"Question: {question}\nSQL:"
         
     | 
| 23 | 
         | 
| 24 | 
         
            +
                inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
         
     | 
| 25 | 
         
            +
                outputs = model.generate(
         
     | 
| 26 | 
         
            +
                    **inputs,
         
     | 
| 27 | 
         
            +
                    max_new_tokens=256,
         
     | 
| 28 | 
         
            +
                    temperature=0.2,  # Low temp for deterministic SQL
         
     | 
| 29 | 
         
            +
                    do_sample=False
         
     | 
| 30 | 
         
            +
                )
         
     | 
| 31 | 
         
             
                sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
         
     | 
| 32 | 
         
             
                return sql_query
         
     | 
| 33 | 
         | 
| 34 | 
         
            +
            # Define Gradio interface (API-like, minimal UI)
         
     | 
| 35 | 
         
             
            iface = gr.Interface(
         
     | 
| 36 | 
         
             
                fn=text_to_sql,
         
     | 
| 37 | 
         
             
                inputs=[
         
     | 
| 
         | 
|
| 39 | 
         
             
                    gr.Textbox(label="Schema (optional)", placeholder="table: columns, ...")
         
     | 
| 40 | 
         
             
                ],
         
     | 
| 41 | 
         
             
                outputs="text",
         
     | 
| 42 | 
         
            +
                title="Text-to-SQL Converter",
         
     | 
| 43 | 
         
            +
                description="Convert natural language questions into SQL queries using the premai-io/prem-1B-SQL model."
         
     | 
| 44 | 
         
             
            )
         
     | 
| 45 | 
         | 
| 46 | 
         
            +
            # Launch (for Spaces: set share=False, HF will handle the endpoint)
         
     | 
| 47 | 
         
             
            iface.launch()
         
     | 
    	
        requirments.txt
    CHANGED
    
    | 
         @@ -1,2 +1,3 @@ 
     | 
|
| 1 | 
         
            -
             
     | 
| 2 | 
         
            -
             
     | 
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            transformers
         
     | 
| 2 | 
         
            +
            torch
         
     | 
| 3 | 
         
            +
            gradio
         
     |