Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import torch
|
| 4 |
-
|
| 5 |
-
# Load the pre-trained model and tokenizer from Hugging Face Model Hub
|
| 6 |
-
model_id = "HridaAI/Hrida-T2SQL-3B-128k-V0.1"
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, trust_remote_code=True)
|
| 9 |
-
|
| 10 |
-
# Define the function to generate SQL query from natural language input
|
| 11 |
-
def generate_sql(query):
|
| 12 |
-
# Tokenize input
|
| 13 |
-
inputs = tokenizer(query, return_tensors="pt")
|
| 14 |
-
|
| 15 |
-
# Generate the SQL query
|
| 16 |
-
outputs = model.generate(**inputs, max_new_tokens=256)
|
| 17 |
-
|
| 18 |
-
# Decode the generated output into a string
|
| 19 |
-
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 20 |
-
|
| 21 |
-
return sql_query
|
| 22 |
-
"""
|
| 23 |
-
# Create the Gradio interface
|
| 24 |
-
iface = gr.Interface(
|
| 25 |
-
fn=generate_sql,
|
| 26 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter your natural language question here..."),
|
| 27 |
-
outputs="text",
|
| 28 |
-
title="Text to SQL Converter",
|
| 29 |
-
description="Convert natural language questions into SQL queries using the Hrida-T2SQL-3B model."
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
# Launch the interface
|
| 33 |
-
iface.launch(server_port=8080)
|
| 34 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
"""
|
| 37 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
|
@@ -54,4 +60,4 @@ demo = gr.ChatInterface(
|
|
| 54 |
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
| 57 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
+
"""
|
| 7 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def respond(
|
| 11 |
+
message,
|
| 12 |
+
history: list[tuple[str, str]],åç
|
| 13 |
+
system_message,
|
| 14 |
+
max_tokens,
|
| 15 |
+
temperature,
|
| 16 |
+
top_p,
|
| 17 |
+
):
|
| 18 |
+
messages = [{"role": "system", "content": system_message}]
|
| 19 |
+
|
| 20 |
+
for val in history:
|
| 21 |
+
if val[0]:
|
| 22 |
+
messages.append({"role": "user", "content": val[0]})
|
| 23 |
+
if val[1]:
|
| 24 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
+
|
| 26 |
+
messages.append({"role": "user", "content": message})
|
| 27 |
+
|
| 28 |
+
response = ""
|
| 29 |
+
|
| 30 |
+
for message in client.chat_completion(
|
| 31 |
+
messages,
|
| 32 |
+
max_tokens=max_tokens,
|
| 33 |
+
stream=True,
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
top_p=top_p,
|
| 36 |
+
):
|
| 37 |
+
token = message.choices[0].delta.content
|
| 38 |
+
|
| 39 |
+
response += token
|
| 40 |
+
yield response
|
| 41 |
|
| 42 |
"""
|
| 43 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
+
demo.launch()
|