JerameeUC commited on
Commit
8a5750e
·
1 Parent(s): 4e800aa

added space_app.py

Browse files
Files changed (1) hide show
  1. space_app.py +41 -0
space_app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ MODEL_NAME = os.getenv("HF_MODEL_GENERATION", "distilgpt2")
6
+
7
+ _pipe = None
8
+ def _get_pipe():
9
+ global _pipe
10
+ if _pipe is None:
11
+ _pipe = pipeline("text-generation", model=MODEL_NAME)
12
+ return _pipe
13
+
14
+ def chat_fn(message, max_new_tokens=128, temperature=0.8, top_p=0.95):
15
+ message = (message or "").strip()
16
+ if not message:
17
+ return "Please type something!"
18
+ pipe = _get_pipe()
19
+ out = pipe(
20
+ message,
21
+ max_new_tokens=int(max_new_tokens),
22
+ do_sample=True,
23
+ temperature=float(temperature),
24
+ top_p=float(top_p),
25
+ pad_token_id=50256
26
+ )
27
+ return out[0]["generated_text"]
28
+
29
+ with gr.Blocks(title="Agentic-Chat-bot") as demo:
30
+ gr.Markdown("# 🤖 Agentic Chat Bot\nGradio + Transformers demo")
31
+ prompt = gr.Textbox(label="Prompt", placeholder="Ask me anything…", lines=4)
32
+ out = gr.Textbox(label="Response", lines=6)
33
+ max_new = gr.Slider(32, 512, 128, 1, label="Max new tokens")
34
+ temp = gr.Slider(0.1, 1.5, 0.8, 0.05, label="Temperature")
35
+ topp = gr.Slider(0.1, 1.0, 0.95, 0.05, label="Top-p")
36
+ btn = gr.Button("Send")
37
+ btn.click(chat_fn, [prompt, max_new, temp, topp], out)
38
+ prompt.submit(chat_fn, [prompt, max_new, temp, topp], out)
39
+
40
+ if __name__ == "__main__":
41
+ demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))