- app.py +43 -61
- requirements.txt +4 -1
app.py
CHANGED
@@ -1,64 +1,46 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Model and tokenizer loading
|
6 |
+
model_id = "cheberle/autotrain-35swc-b4r9z"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
9 |
+
|
10 |
+
# Move model to GPU if available
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
model = model.to(device)
|
13 |
+
|
14 |
+
def predict(text):
|
15 |
+
# Tokenize input
|
16 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
17 |
+
|
18 |
+
# Move inputs to same device as model
|
19 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
20 |
+
|
21 |
+
# Get prediction
|
22 |
+
with torch.no_grad():
|
23 |
+
outputs = model(**inputs)
|
24 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
25 |
+
|
26 |
+
# Get prediction probabilities and labels
|
27 |
+
probs = predictions[0].tolist()
|
28 |
+
labels = model.config.id2label
|
29 |
+
|
30 |
+
# Create formatted output
|
31 |
+
results = {labels[i]: float(probs[i]) for i in range(len(probs))}
|
32 |
+
|
33 |
+
return results
|
34 |
+
|
35 |
+
# Create Gradio interface
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=predict,
|
38 |
+
inputs=gr.Textbox(label="Input Text"),
|
39 |
+
outputs=gr.Label(label="Prediction"),
|
40 |
+
title="Model Prediction Interface",
|
41 |
+
description=f"Enter text to get predictions from {model_id}",
|
42 |
+
examples=["Example text to try"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
)
|
44 |
|
45 |
+
# Launch the interface
|
46 |
+
iface.launch()
|
|
requirements.txt
CHANGED
@@ -1 +1,4 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
gradio
|
3 |
+
torch
|
4 |
+
transformers
|