Banu007 commited on
Commit
142e103
·
verified ·
1 Parent(s): 82ad9cb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
+
5
+ # Initialize the Hugging Face Inference Client
6
+ # Make sure to add your HF_TOKEN in the Space Settings if it's a gated model
7
+ client = InferenceClient(
8
+ model="meta-llama/Llama-3.3-70B-Instruct",
9
+ token=os.getenv("HF_TOKEN")
10
+ )
11
+
12
+ def respond(message, chat_history, system_message, max_tokens, temperature, top_p):
13
+ # Format the chat history for the conversational model
14
+ messages = [{"role": "system", "content": system_message}]
15
+
16
+ for val in chat_history:
17
+ if val[0]:
18
+ messages.append({"role": "user", "content": val[0]})
19
+ if val[1]:
20
+ messages.append({"role": "assistant", "content": val[1]})
21
+
22
+ messages.append({"role": "user", "content": message})
23
+
24
+ response = ""
25
+
26
+ # Stream the response back from the Llama 3.3 model
27
+ for msg in client.chat_completion(
28
+ messages,
29
+ max_tokens=max_tokens,
30
+ stream=True,
31
+ temperature=temperature,
32
+ top_p=top_p,
33
+ ):
34
+ token = msg.choices[0].delta.content
35
+ if token:
36
+ response += token
37
+ yield response
38
+
39
+ # Define a clean Gradio Chat Interface
40
+ demo = gr.ChatInterface(
41
+ respond,
42
+ additional_inputs=[
43
+ gr.Textbox(value="You are a helpful, smart AI assistant powered by Llama 3.3.", label="System Message"),
44
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
45
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
46
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
47
+ ],
48
+ title="Llama 3.3 70B Instruct - Agent Demo",
49
+ description="A simple conversational agent interface leveraging Meta's Llama-3.3-70B-Instruct model.",
50
+ )
51
+
52
+ if __name__ == "__main__":
53
+ demo.launch()
54
+