neuralworm commited on
Commit
911fa18
·
verified ·
1 Parent(s): ae40801

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -22
app.py CHANGED
@@ -1,25 +1,70 @@
1
  import gradio as gr
2
- from huggingface_hub.utils import HfHubHTTPError
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- def predict(message, history):
5
  try:
6
- # Load the model interface directly using the load method
7
- chat_interface = gr.Interface.load("models/meta-llama/Meta-Llama-3.1-8B")
8
-
9
- # Get the prediction using the correct method call
10
- response = chat_interface.predict(message)
11
- history.append((message, response))
12
- return "", history
13
-
14
- except HfHubHTTPError as e:
15
- if e.response.status_code == 504:
16
- return "Server overloaded. Please try again later.", history
17
- else:
18
- raise e
19
-
20
- with gr.Blocks() as demo:
21
- chatbot = gr.Chatbot()
22
- msg = gr.Textbox()
23
- clear = gr.ClearButton([msg, chatbot])
24
-
25
- msg.submit(predict, [msg, chatbot], [msg, chatbot])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import os
4
+
5
+ # Ensure the required library is installed
6
+ os.system("pip install minijinja")
7
+
8
+ """
9
+ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
10
+ """
11
+ client = InferenceClient("meta-llama/Meta-Llama-3.1-8B")
12
+
13
+
14
+ def respond(
15
+ message,
16
+ history: list[tuple[str, str]],
17
+ system_message,
18
+ max_tokens,
19
+ temperature,
20
+ top_p,
21
+ ):
22
+ messages = [system_message]
23
+
24
+ for val in history:
25
+ if val[0]:
26
+ messages.append(val[0])
27
+ if val[1]:
28
+ messages.append(val[1])
29
+
30
+ messages.append(message)
31
+
32
+ response = ""
33
 
 
34
  try:
35
+ for message in client.chat_completion(
36
+ messages,
37
+ max_tokens=max_tokens,
38
+ stream=True,
39
+ temperature=temperature,
40
+ top_p=top_p,
41
+ ):
42
+ token = message.choices[0].delta.content
43
+
44
+ response += token
45
+ yield response
46
+ except Exception as e:
47
+ yield f"Error: {str(e)}"
48
+
49
+
50
+ """
51
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
52
+ """
53
+ demo = gr.ChatInterface(
54
+ respond,
55
+ additional_inputs=[
56
+ gr.Textbox(value="", label="System message"),
57
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
58
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
59
+ gr.Slider(
60
+ minimum=0.1,
61
+ maximum=1.0,
62
+ value=0.95,
63
+ step=0.05,
64
+ label="Top-p (nucleus sampling)",
65
+ ),
66
+ ],
67
+ )
68
+
69
+ if __name__ == "__main__":
70
+ demo.launch()