azazel1805 commited on
Commit
0578586
·
verified ·
1 Parent(s): a4c6854

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
 
4
 
5
  # Fetch the token from the environment variable
6
  hf_token = os.getenv("HF_TOKEN")
@@ -13,29 +14,39 @@ try:
13
  except Exception as e:
14
  raise Exception(f"Failed to initialize InferenceClient: {str(e)}")
15
 
16
- # Chat function with minimal text_generation call
17
  def chat_with_bot(user_input, history=[]):
18
- try:
19
- response = client.text_generation(
20
- prompt=user_input,
21
- model="meta-llama/Llama-3.2-1B"
22
- )
23
- history.append([user_input, response])
24
- chat_output = "\n".join([f"You: {h[0]}\nBot: {h[1]}" for h in history])
25
- return chat_output, history
26
- except Exception as e:
27
- return f"Chat Error: {str(e)}", history
 
 
 
 
 
28
 
29
  # Test the API
30
  def test_api():
31
- try:
32
- response = client.text_generation(
33
- prompt="Hi",
34
- model="meta-llama/Llama-3.2-1B"
35
- )
36
- return response
37
- except Exception as e:
38
- return f"API Test Error: {str(e)}"
 
 
 
 
 
39
 
40
  # Create Gradio interface
41
  interface = gr.Interface(
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
+ import time
5
 
6
  # Fetch the token from the environment variable
7
  hf_token = os.getenv("HF_TOKEN")
 
14
  except Exception as e:
15
  raise Exception(f"Failed to initialize InferenceClient: {str(e)}")
16
 
17
+ # Chat function with retry logic
18
  def chat_with_bot(user_input, history=[]):
19
+ max_retries = 3
20
+ for attempt in range(max_retries):
21
+ try:
22
+ response = client.text_generation(
23
+ prompt=user_input,
24
+ model="meta-llama/Llama-3.2-1B"
25
+ )
26
+ history.append([user_input, response])
27
+ chat_output = "\n".join([f"You: {h[0]}\nBot: {h[1]}" for h in history])
28
+ return chat_output, history
29
+ except Exception as e:
30
+ if attempt < max_retries - 1:
31
+ time.sleep(2) # Wait before retrying
32
+ continue
33
+ return f"Chat Error: {str(e)} (after {max_retries} attempts)", history
34
 
35
  # Test the API
36
  def test_api():
37
+ max_retries = 3
38
+ for attempt in range(max_retries):
39
+ try:
40
+ response = client.text_generation(
41
+ prompt="Hi",
42
+ model="meta-llama/Llama-3.2-1B"
43
+ )
44
+ return response
45
+ except Exception as e:
46
+ if attempt < max_retries - 1:
47
+ time.sleep(2) # Wait before retrying
48
+ continue
49
+ return f"API Test Error: {str(e)} (after {max_retries} attempts)"
50
 
51
  # Create Gradio interface
52
  interface = gr.Interface(