Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,20 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
|
4 |
-
# Define
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
try:
|
19 |
-
return InferenceClient(model_name)
|
20 |
-
except Exception as e:
|
21 |
-
print(f"Error creating client for {model_name}: {e}")
|
22 |
-
return None
|
23 |
|
24 |
def respond(
|
25 |
message,
|
@@ -30,45 +25,65 @@ def respond(
|
|
30 |
temperature,
|
31 |
top_p,
|
32 |
):
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
if not
|
38 |
-
return "Error:
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
messages.append({"role": "user", "content": message})
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
def create_chat_interface():
|
66 |
"""Create Gradio ChatInterface with model selection."""
|
67 |
demo = gr.ChatInterface(
|
68 |
respond,
|
69 |
additional_inputs=[
|
70 |
-
gr.Textbox(value="You are a
|
71 |
-
gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
73 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
74 |
gr.Slider(
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
|
4 |
+
# Define all pipelines
|
5 |
+
def load_pipelines():
|
6 |
+
pipelines = {
|
7 |
+
"GPT-2 Original": pipeline("text-generation", model="gpt2"),
|
8 |
+
"GPT-2 Medium": pipeline("text-generation", model="gpt2-medium"),
|
9 |
+
"DistilGPT-2": pipeline("text-generation", model="distilgpt2"),
|
10 |
+
"German GPT-2": pipeline("text-generation", model="german-nlp-group/german-gpt2"),
|
11 |
+
"German Wechsel GPT-2": pipeline("text-generation", model="benjamin/gpt2-wechsel-german"),
|
12 |
+
"T5 Base": pipeline("text-generation", model="t5-base"),
|
13 |
+
"T5 Large": pipeline("text-generation", model="t5-large"),
|
14 |
+
"Text Classification": pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english"),
|
15 |
+
"Sentiment Analysis": pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
16 |
+
}
|
17 |
+
return pipelines
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def respond(
|
20 |
message,
|
|
|
25 |
temperature,
|
26 |
top_p,
|
27 |
):
|
28 |
+
# Load pipelines
|
29 |
+
pipelines = load_pipelines()
|
30 |
+
pipe = pipelines.get(model_name)
|
31 |
|
32 |
+
if not pipe:
|
33 |
+
return "Error: Model not found."
|
34 |
|
35 |
+
# For text generation models
|
36 |
+
if model_name in ["GPT-2 Original", "GPT-2 Medium", "DistilGPT-2",
|
37 |
+
"German GPT-2", "German Wechsel GPT-2",
|
38 |
+
"T5 Base", "T5 Large"]:
|
39 |
+
# Prepare full prompt
|
40 |
+
full_history = ' '.join([f"{msg[0]} {msg[1] or ''}" for msg in history]) if history else ''
|
41 |
+
full_prompt = f"{system_message}\n{full_history}\nUser: {message}\nAssistant:"
|
|
|
42 |
|
43 |
+
try:
|
44 |
+
response = pipe(
|
45 |
+
full_prompt,
|
46 |
+
max_length=len(full_prompt) + max_tokens,
|
47 |
+
temperature=temperature,
|
48 |
+
top_p=top_p,
|
49 |
+
num_return_sequences=1
|
50 |
+
)[0]['generated_text']
|
51 |
+
|
52 |
+
# Extract just the new assistant response
|
53 |
+
assistant_response = response[len(full_prompt):].strip()
|
54 |
+
return assistant_response
|
55 |
+
except Exception as e:
|
56 |
+
return f"Generation error: {e}"
|
57 |
+
|
58 |
+
# For classification and sentiment models
|
59 |
+
elif model_name == "Text Classification":
|
60 |
+
try:
|
61 |
+
result = pipe(message)[0]
|
62 |
+
return f"Classification: {result['label']} (Confidence: {result['score']:.2f})"
|
63 |
+
except Exception as e:
|
64 |
+
return f"Classification error: {e}"
|
65 |
+
|
66 |
+
elif model_name == "Sentiment Analysis":
|
67 |
+
try:
|
68 |
+
result = pipe(message)[0]
|
69 |
+
return f"Sentiment: {result['label']} (Confidence: {result['score']:.2f})"
|
70 |
+
except Exception as e:
|
71 |
+
return f"Sentiment analysis error: {e}"
|
72 |
|
73 |
def create_chat_interface():
|
74 |
"""Create Gradio ChatInterface with model selection."""
|
75 |
demo = gr.ChatInterface(
|
76 |
respond,
|
77 |
additional_inputs=[
|
78 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
79 |
+
gr.Dropdown(
|
80 |
+
["GPT-2 Original", "GPT-2 Medium", "DistilGPT-2",
|
81 |
+
"German GPT-2", "German Wechsel GPT-2",
|
82 |
+
"T5 Base", "T5 Large",
|
83 |
+
"Text Classification", "Sentiment Analysis"],
|
84 |
+
value="GPT-2 Original",
|
85 |
+
label="Select Model"
|
86 |
+
),
|
87 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
88 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
89 |
gr.Slider(
|