Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load the saved tokenizer and model
|
| 5 |
+
tokenizer = GPT2Tokenizer.from_pretrained("Rehman1603/new_crm_fine_tuned_gpt2_model")
|
| 6 |
+
model = GPT2LMHeadModel.from_pretrained("Rehman1603/new_crm_fine_tuned_gpt2_model")
|
| 7 |
+
|
| 8 |
+
# Function to generate responses
|
| 9 |
+
def generate_response(question, max_length=150):
|
| 10 |
+
input_text = f"<startofstring> {question} <bot>:"
|
| 11 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 12 |
+
output = model.generate(
|
| 13 |
+
input_ids,
|
| 14 |
+
max_length=max_length,
|
| 15 |
+
num_return_sequences=1,
|
| 16 |
+
no_repeat_ngram_size=2, # Prevent repeating n-grams
|
| 17 |
+
top_k=50, # Limit sampling to the top-k tokens
|
| 18 |
+
top_p=0.95, # Use nucleus sampling
|
| 19 |
+
temperature=0.7, # Control randomness
|
| 20 |
+
do_sample=True, # Enable sampling
|
| 21 |
+
)
|
| 22 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 23 |
+
# Extract only the bot's response
|
| 24 |
+
response = response.split("<bot>:")[-1].strip()
|
| 25 |
+
return response
|
| 26 |
+
|
| 27 |
+
# Function to handle chat interaction
|
| 28 |
+
def chat(message, history):
|
| 29 |
+
# Generate a response using the model
|
| 30 |
+
response = generate_response(message)
|
| 31 |
+
return response
|
| 32 |
+
|
| 33 |
+
# Example questions
|
| 34 |
+
examples = [
|
| 35 |
+
"Hello! Can I get more info?",
|
| 36 |
+
"Restaurant",
|
| 37 |
+
"online or offline?",
|
| 38 |
+
"isky charges kia ha",
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
# Gradio ChatInterface
|
| 42 |
+
demo = gr.ChatInterface(
|
| 43 |
+
fn=chat, # Function to handle chat
|
| 44 |
+
examples=examples, # Example questions
|
| 45 |
+
title="Chat with the Fine-Tuned GPT-2 Model", # Title of the interface
|
| 46 |
+
description="Ask me anything about the software!", # Description
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Launch the Gradio app
|
| 50 |
+
demo.launch()
|