YaserDS-777 commited on
Commit
1f2af73
·
verified ·
1 Parent(s): 0627c22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -21
app.py CHANGED
@@ -1,30 +1,74 @@
1
- import os
2
  import gradio as gr
3
- from transformers import pipeline
 
4
 
5
- # Get the Hugging Face API token from environment variables
6
- api_token = os.getenv("HUGGINGFACE_API_TOKEN_V")
7
  print('----------------',api_token,'-----------------')
8
  # Check if the API token is set
9
  if not api_token:
10
  raise ValueError("API token is not set. Please set the HUGGINGFACE_API_TOKEN environment variable.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Initialize the text generation pipeline with authentication
13
- pipe = pipeline("text-generation", model="meta-llama/Meta-Llama-3.1-405B", use_auth_token=True)
14
-
15
- # Define the function to generate text
16
- def generate_text(prompt):
17
- result = pipe(prompt, max_length=50, num_return_sequences=1)
18
- return result[0]['generated_text']
19
-
20
- # Create a Gradio interface
21
- iface = gr.Interface(
22
- fn=generate_text,
23
- inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your prompt here..."),
24
- outputs="text",
25
- title="Meta-Llama Text Generation",
26
- description="Generate text using the Meta-Llama 3.1 405B model."
27
  )
28
 
29
- # Launch the interface
30
- iface.launch()
 
1
+
2
  import gradio as gr
3
+ import requests
4
+ import os
5
 
6
+ TOKEN = os.getenv("HUGGINGFACE_API_TOKEN_V")
 
7
  print('----------------',api_token,'-----------------')
8
  # Check if the API token is set
9
  if not api_token:
10
  raise ValueError("API token is not set. Please set the HUGGINGFACE_API_TOKEN environment variable.")
11
+ import gradio as gr
12
+ import requests
13
+ import os
14
+
15
+ def respond(
16
+ message,
17
+ history: list[tuple[str, str]],
18
+ system_message,
19
+ max_tokens,
20
+ temperature,
21
+ top_p,
22
+ ):
23
+ messages = [{"role": "system", "content": system_message}]
24
+
25
+ for val in history:
26
+ if val[0]:
27
+ messages.append({"role": "user", "content": val[0]})
28
+ if val[1]:
29
+ messages.append({"role": "assistant", "content": val[1]})
30
+
31
+ messages.append({"role": "user", "content": message})
32
+
33
+ headers = {
34
+ "Authorization": f"Bearer {TOKEN}",
35
+ "Content-Type": "application/json"
36
+ }
37
+
38
+ payload = {
39
+ "model": "meta-llama/Meta-Llama-3.1-405B-Instruct-FP8",
40
+ "max_tokens": max_tokens,
41
+ "temperature": temperature,
42
+ "top_p": top_p,
43
+ "messages": messages
44
+ }
45
+
46
+ response = requests.post("https://api-inference.huggingface.co/v1/chat/completions", headers=headers, json=payload, stream=True)
47
+
48
+ response_text = ""
49
+ for chunk in response.iter_content(chunk_size=None):
50
+ if chunk:
51
+ response_text += chunk.decode('utf-8')
52
+ yield response_text
53
+
54
+ theme="Nymbo/Nymbo_Theme"
55
 
56
+ demo = gr.ChatInterface(
57
+ respond,
58
+ theme=theme,
59
+ additional_inputs=[
60
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
61
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
62
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
63
+ gr.Slider(
64
+ minimum=0.1,
65
+ maximum=1.0,
66
+ value=0.95,
67
+ step=0.05,
68
+ label="Top-p (nucleus sampling)",
69
+ ),
70
+ ],
71
  )
72
 
73
+ if __name__ == "__main__":
74
+ demo.launch()