Spaces:
Build error
Build error
Update app.py
Browse files
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
|
| 17 |
def chat_with_bot(user_input, history=[]):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Test the API
|
| 30 |
def test_api():
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|