Canstralian commited on
Commit
473cf5f
·
verified ·
1 Parent(s): 1d8a208

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +49 -0
main.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+
4
+ # Initialize the Hugging Face Inference Client
5
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
+
7
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
8
+ """
9
+ Handles user input and generates a response using the Hugging Face model.
10
+ """
11
+ try:
12
+ # Construct the conversation context
13
+ messages = [{"role": "system", "content": system_message}]
14
+ for user_msg, assistant_msg in history:
15
+ if user_msg:
16
+ messages.append({"role": "user", "content": user_msg})
17
+ if assistant_msg:
18
+ messages.append({"role": "assistant", "content": assistant_msg})
19
+ messages.append({"role": "user", "content": message})
20
+
21
+ # Generate the response
22
+ response = ""
23
+ for message in client.chat_completion(
24
+ messages,
25
+ max_tokens=max_tokens,
26
+ temperature=temperature,
27
+ top_p=top_p,
28
+ stream=True
29
+ ):
30
+ token = message.choices[0].delta.content
31
+ response += token
32
+ yield response
33
+ except Exception as e:
34
+ yield f"Error: {str(e)}"
35
+
36
+ # Create the Gradio interface
37
+ demo = gr.ChatInterface(
38
+ respond,
39
+ additional_inputs=[
40
+ gr.Textbox(value="You are a helpful assistant.", label="System Message"),
41
+ gr.Slider(minimum=1, maximum=2048, value=512, label="Max Tokens"),
42
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Temperature"),
43
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-p (Nucleus Sampling)"),
44
+ ]
45
+ )
46
+
47
+ # Launch the app
48
+ if __name__ == "__main__":
49
+ demo.launch()