alex-abb commited on
Commit
57d46c6
·
verified ·
1 Parent(s): afd9f6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -73
app.py CHANGED
@@ -1,80 +1,44 @@
1
- from pyexpat.errors import messages
2
  import gradio as gr
3
- from huggingface_hub import InferenceClient
4
- import spaces
5
- """
6
- 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
7
- """
8
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
- @spaces.GPU
10
-
11
-
12
 
13
 
14
-
15
-
16
-
17
- def respond(
18
- message,
19
- history: list[tuple[str, str]],
20
- system_message,
21
- max_tokens,
22
- temperature,
23
- top_p,
24
-
25
- ):
26
- messages = [{"role": "system", "content": system_message}]
27
-
28
-
 
 
 
29
 
30
- for val in history:
31
- if val[0]:
32
- messages.append({"role": "user", "content": val[0]})
33
- if val[1]:
34
- messages.append({"role": "assistant", "content": val[1]})
35
-
36
- messages.append({"role": "user", "content": message})
37
-
38
- response = ""
39
-
40
 
41
- for message in client.chat_completion(
42
- messages,
43
- max_tokens=max_tokens,
44
- stream=True,
45
- temperature=temperature,
46
- top_p=top_p,
47
-
48
- ):
49
- token = message.choices[0].delta.content
50
-
51
- response += token
52
- yield response
53
-
54
-
55
-
56
-
57
- """
58
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
59
- """
60
- demo = gr.ChatInterface(
61
- respond,
62
- additional_inputs=[
63
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
64
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
65
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
66
- gr.Slider(
67
- minimum=0.1,
68
- maximum=1.0,
69
- value=0.95,
70
- step=0.05,
71
- label="Top-p (nucleus sampling)",
72
-
73
- ),
74
- ],
75
-
76
  )
77
 
78
-
79
- if __name__ == "__main__":
80
- demo.launch()
 
 
1
  import gradio as gr
2
+ import requests
3
+ import os
4
+ import spaces
 
 
 
 
 
 
5
 
6
 
7
+ API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
8
+ api_token = os.environ.get("TOKEN")
9
+ headers = {"Authorization": f"Bearer {api_token}"}
10
+ @spaces.GPU
11
+ def query(payload):
12
+ response = requests.post(API_URL, headers=headers, json=payload)
13
+ return response.json()
14
+
15
+ def generate_response(prompt):
16
+ payload = {
17
+ "inputs": prompt,
18
+ "parameters": {
19
+ "max_new_tokens": 100,
20
+ "temperature": 0.7,
21
+ "top_p": 0.95,
22
+ "do_sample": True
23
+ }
24
+ }
25
 
26
+ response = query(payload)
 
 
 
 
 
 
 
 
 
27
 
28
+ if isinstance(response, list) and len(response) > 0:
29
+ return response[0].get('generated_text', '')
30
+ elif isinstance(response, dict) and 'generated_text' in response:
31
+ return response['generated_text']
32
+ return "Désolé, je n'ai pas pu générer de réponse."
33
+
34
+ def chatbot(message, history):
35
+ response = generate_response(message)
36
+ return response
37
+
38
+ iface = gr.ChatInterface(
39
+ fn=chatbot,
40
+ title="Chatbot Meta-Llama-3-8B-Instruct",
41
+ description="Interagissez avec le modèle Meta-Llama-3-8B-Instruct."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  )
43
 
44
+ iface.launch()