Upload 3 files
Browse files- app.py +55 -0
- best_model.pt +3 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from model_utils import load_model, generate_text
|
4 |
+
|
5 |
+
# Initialize model
|
6 |
+
try:
|
7 |
+
model_path = "best_model.pt" # Local model file
|
8 |
+
model = load_model(model_path)
|
9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
10 |
+
model = model.to(device)
|
11 |
+
except Exception as e:
|
12 |
+
print(f"Error loading model: {e}")
|
13 |
+
model = None
|
14 |
+
|
15 |
+
def predict(prompt, max_tokens, temperature, top_k):
|
16 |
+
"""Wrapper function for Gradio interface"""
|
17 |
+
if model is None:
|
18 |
+
return "Error: Model failed to load. Please check the logs."
|
19 |
+
|
20 |
+
try:
|
21 |
+
generated_text = generate_text(
|
22 |
+
model=model,
|
23 |
+
prompt=prompt,
|
24 |
+
max_new_tokens=max_tokens,
|
25 |
+
temperature=temperature,
|
26 |
+
top_k=top_k
|
27 |
+
)
|
28 |
+
return generated_text
|
29 |
+
except Exception as e:
|
30 |
+
return f"Error during generation: {str(e)}"
|
31 |
+
|
32 |
+
# Create Gradio interface
|
33 |
+
demo = gr.Interface(
|
34 |
+
fn=predict,
|
35 |
+
inputs=[
|
36 |
+
gr.Textbox(label="Enter your prompt", lines=3),
|
37 |
+
gr.Slider(minimum=1, maximum=200, value=50, step=1, label="Max Tokens"),
|
38 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.8, step=1, label="Temperature"),
|
39 |
+
gr.Slider(minimum=1, maximum=100, value=40, step=1, label="Top-k"),
|
40 |
+
],
|
41 |
+
outputs=gr.Textbox(label="Generated Text", lines=5),
|
42 |
+
title="GPT Text Generation",
|
43 |
+
description="Enter a text prompt and the model will generate a continuation.",
|
44 |
+
examples=[
|
45 |
+
["The quick brown fox", 50, 0.8, 40],
|
46 |
+
["Once upon a time", 100, 0.9, 50],
|
47 |
+
["In the distant future", 75, 0.7, 30],
|
48 |
+
],
|
49 |
+
)
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
demo.launch(share=True) # You can set share=False if you don't want to share publicly
|
53 |
+
else:
|
54 |
+
# For Hugging Face Spaces
|
55 |
+
app = demo.launch(share=False)
|
best_model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a7a7c8f4600dd1463f53caa8e594dc828d421e9e9001ed42aeb6b6ee8064abf7
|
3 |
+
size 921869805
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
tiktoken
|
3 |
+
gradio
|