Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the model and tokenizer from Hugging Face
|
6 |
+
model_name = "Rehman1603/airline_guidenece" # Replace with your Hugging Face model name
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
9 |
+
|
10 |
+
# Prepare the model for inference
|
11 |
+
model.eval()
|
12 |
+
|
13 |
+
# Define the Alpaca prompt format
|
14 |
+
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
15 |
+
|
16 |
+
### Response:
|
17 |
+
{}"""
|
18 |
+
|
19 |
+
def chat_with_model(instruction):
|
20 |
+
# Format the input with the Alpaca prompt
|
21 |
+
formatted_input = alpaca_prompt.format(instruction)
|
22 |
+
|
23 |
+
# Tokenize the input
|
24 |
+
inputs = tokenizer(
|
25 |
+
formatted_input,
|
26 |
+
return_tensors="pt",
|
27 |
+
).to("cuda")
|
28 |
+
|
29 |
+
# Generate the response
|
30 |
+
outputs = model.generate(**inputs, max_new_tokens=64, use_cache=True)
|
31 |
+
decoded_output = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
32 |
+
|
33 |
+
# Extract the response part after "### Response:"
|
34 |
+
response_start = decoded_output.find("### Response:") + len("### Response:")
|
35 |
+
response_text = decoded_output[response_start:].strip()
|
36 |
+
|
37 |
+
return response_text
|
38 |
+
|
39 |
+
# Create a Gradio interface
|
40 |
+
interface = gr.Interface(
|
41 |
+
fn=chat_with_model,
|
42 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your instruction here..."),
|
43 |
+
outputs="text",
|
44 |
+
title="Airline Guidance Chatbot",
|
45 |
+
description="Ask questions about airline guidance and get responses from the model.",
|
46 |
+
)
|
47 |
+
|
48 |
+
# Launch the Gradio app
|
49 |
+
interface.launch()
|