Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
# Load the pre-trained model from Hugging Face
|
6 |
+
|
7 |
+
import torch
|
8 |
+
from peft import AutoPeftModelForCausalLM
|
9 |
+
from transformers import AutoTokenizer, pipeline
|
10 |
+
|
11 |
+
peft_model_id = "jinhybr/code-llama-7b-text-to-sql"
|
12 |
+
# peft_model_id = args.output_dir
|
13 |
+
|
14 |
+
# Load Model with PEFT adapter
|
15 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
16 |
+
peft_model_id,
|
17 |
+
device_map="auto",
|
18 |
+
torch_dtype=torch.float16
|
19 |
+
)
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
|
21 |
+
# load into pipeline
|
22 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
23 |
+
|
24 |
+
|
25 |
+
# Define the Gradio interface
|
26 |
+
def translate_to_sql(question):
|
27 |
+
strA = 'You are a text to SQL query translator. Users will ask you questions in English and you will generate a SQL query based on the provided SCHEMA.\nSCHEMA:\nCREATE TABLE table_17429402_7 (school VARCHAR, last_occ_championship VARCHAR)'
|
28 |
+
combined_json_data = [{'content': strA, 'role': 'system'}, {'content': question, 'role': 'user'}]
|
29 |
+
prompt = pipe.tokenizer.apply_chat_template(combined_json_data, tokenize=False, add_generation_prompt=True)
|
30 |
+
outputs = pipe(prompt, max_new_tokens=256, do_sample=False, temperature=0.1, top_k=50, top_p=0.1, eos_token_id=pipe.tokenizer.eos_token_id, pad_token_id=pipe.tokenizer.pad_token_id)
|
31 |
+
return outputs[0]['generated_text'][len(prompt):].strip()
|
32 |
+
|
33 |
+
question_input = gr.inputs.Textbox(lines=7, label="Enter your question")
|
34 |
+
output_text = gr.outputs.Textbox(label="Generated SQL Query")
|
35 |
+
|
36 |
+
# Create the Gradio interface
|
37 |
+
gr.Interface(fn=translate_to_sql, inputs=question_input, outputs=output_text, title="Text to SQL Translator", description="Translate English questions to SQL queries.").launch()
|
38 |
+
|
39 |
+
# Create the Gradio interface
|
40 |
+
gr.Interface(fn=classify_text, inputs=inputs, outputs=outputs, title="Sentiment Analysis", description="Predict the sentiment of text.").launch()
|