RandomOnHuggingFace commited on
Commit
d79fa5e
·
verified ·
1 Parent(s): 9d4c6d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -28
app.py CHANGED
@@ -1,18 +1,10 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # Use the correct GPT-2 model ("gpttrash" in this case)
5
  client = InferenceClient("gpt2")
6
 
7
- def respond(
8
- message,
9
- history: list[tuple[str, str]],
10
- system_message, # This will be ignored
11
- max_tokens,
12
- temperature,
13
- top_p,
14
- ):
15
- # Remove the system message from the list of messages
16
  messages = []
17
 
18
  # Add the conversation history (user and assistant exchanges)
@@ -27,33 +19,32 @@ def respond(
27
 
28
  response = ""
29
 
30
- # Get the model's response
31
- for message in client.chat_completion(
32
  messages,
33
  max_tokens=max_tokens,
34
  stream=True,
35
  temperature=temperature,
36
  top_p=top_p,
37
  ):
38
- token = message.choices[0].delta.content
39
  response += token
40
  yield response
41
 
42
- # Create the Gradio interface
43
- demo = gr.ChatInterface(
44
- respond,
45
- additional_inputs=[
46
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
47
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
48
- gr.Slider(
49
- minimum=0.1,
50
- maximum=1.0,
51
- value=0.95,
52
- step=0.05,
53
- label="Top-p (nucleus sampling)",
54
- ),
55
- ],
56
- )
57
 
58
  if __name__ == "__main__":
59
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Initialize the Inference Client for the GPT-2 model (or "gpttrash")
5
  client = InferenceClient("gpt2")
6
 
7
+ def respond(message, history, max_tokens, temperature, top_p):
 
 
 
 
 
 
 
 
8
  messages = []
9
 
10
  # Add the conversation history (user and assistant exchanges)
 
19
 
20
  response = ""
21
 
22
+ # Get the model's response using chat completion
23
+ for response_chunk in client.chat_completion(
24
  messages,
25
  max_tokens=max_tokens,
26
  stream=True,
27
  temperature=temperature,
28
  top_p=top_p,
29
  ):
30
+ token = response_chunk.choices[0].delta.content
31
  response += token
32
  yield response
33
 
34
+ # Create Gradio Blocks layout for Hugging Face Spaces
35
+ with gr.Blocks() as demo:
36
+ with gr.Row():
37
+ user_input = gr.Textbox(label="User Input")
38
+ history = gr.State() # Keeps conversation history
39
+ with gr.Row():
40
+ max_tokens_slider = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
41
+ temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
42
+ top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
43
+ with gr.Row():
44
+ output = gr.Textbox(label="Model Output")
45
+
46
+ # Set up the chatbot functionality
47
+ user_input.submit(respond, [user_input, history, max_tokens_slider, temperature_slider, top_p_slider], output)
 
48
 
49
  if __name__ == "__main__":
50
  demo.launch()