Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
+
import torch
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Load token from environment variable
|
7 |
+
token = os.getenv('ACCESS_SECRET')
|
8 |
+
|
9 |
+
# Specify the repository ID
|
10 |
+
model_repo_id = "Ajay12345678980/QA_Chatbot"
|
11 |
+
|
12 |
+
# Load model and tokenizer
|
13 |
+
model = GPT2LMHeadModel.from_pretrained(model_repo_id, use_auth_token=token)
|
14 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_repo_id, use_auth_token=token)
|
15 |
+
|
16 |
+
# Define prediction function
|
17 |
+
def predict(text):
|
18 |
+
inputs = tokenizer.encode(text, return_tensors="pt")
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model.generate(inputs, max_length=50, do_sample=True)
|
21 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
22 |
+
return prediction
|
23 |
+
|
24 |
+
# Set up Gradio interface
|
25 |
+
interface = gr.Interface(
|
26 |
+
fn=predict,
|
27 |
+
inputs="text",
|
28 |
+
outputs="text",
|
29 |
+
title="GPT-2 Text Generation",
|
30 |
+
description="Enter some text and see what the model generates!"
|
31 |
+
)
|
32 |
+
|
33 |
+
# Launch the Gradio app
|
34 |
+
if __name__ == "__main__":
|
35 |
+
interface.launch()
|