Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import requests
|
|
3 |
import os
|
4 |
|
5 |
# Load API URL and token from environment variables
|
6 |
-
API_URL = os.getenv("HF_API_URL", "https://api-inference.huggingface.co/models/
|
7 |
API_TOKEN = os.getenv("HF_API_TOKEN", "your-default-token") # Replace with your actual token for fallback
|
8 |
|
9 |
# Function to call the Hugging Face Inference API
|
@@ -14,6 +14,7 @@ def call_huggingface_api(input_text):
|
|
14 |
print(f"Request sent to: {API_URL}")
|
15 |
print(f"Payload: {payload}")
|
16 |
|
|
|
17 |
response = requests.post(API_URL, headers=headers, json=payload)
|
18 |
|
19 |
print(f"Response Status Code: {response.status_code}")
|
@@ -21,49 +22,62 @@ def call_huggingface_api(input_text):
|
|
21 |
if response.status_code == 200:
|
22 |
data = response.json()
|
23 |
print(f"Response Data: {data}")
|
24 |
-
# Assuming 'answer' and 'confidence'
|
25 |
return f"Question: {input_text}\nAnswer: {data.get('answer', 'No answer found.')}\nConfidence: {data.get('confidence', 'N/A')}"
|
26 |
else:
|
27 |
-
|
28 |
-
|
29 |
-
print(f"Error Response: {error_message}")
|
30 |
-
return error_message
|
31 |
|
32 |
except requests.exceptions.RequestException as e:
|
33 |
-
# Catch network-related errors
|
34 |
error_message = f"Network error during API call: {e}"
|
35 |
print(error_message)
|
36 |
return error_message
|
37 |
|
38 |
except ValueError as e:
|
39 |
-
# Handle invalid JSON response
|
40 |
error_message = f"Error parsing response JSON: {e}"
|
41 |
print(error_message)
|
42 |
return error_message
|
43 |
|
44 |
except KeyError as e:
|
45 |
-
# Handle unexpected response structure or missing keys
|
46 |
error_message = f"KeyError: Missing expected key in response JSON: {e}"
|
47 |
print(error_message)
|
48 |
return error_message
|
49 |
|
50 |
except Exception as e:
|
51 |
-
# Handle any other unexpected errors
|
52 |
error_message = f"Unexpected error during API call: {e}"
|
53 |
print(error_message)
|
54 |
return error_message
|
55 |
|
56 |
|
57 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
gr.Interface(
|
59 |
-
fn=
|
60 |
inputs="text",
|
61 |
outputs="text",
|
62 |
examples=[
|
63 |
["Who is rahul7star?"],
|
64 |
["What does Rahul7star do?"],
|
65 |
-
["Tell me about Rahul7star"]
|
|
|
66 |
],
|
67 |
title="Ask Rahul7star AI",
|
68 |
-
description="Ask questions about
|
69 |
).launch()
|
|
|
3 |
import os
|
4 |
|
5 |
# Load API URL and token from environment variables
|
6 |
+
API_URL = os.getenv("HF_API_URL", "https://api-inference.huggingface.co/models/rahul7star/fastai-rahul-text-model-v02")
|
7 |
API_TOKEN = os.getenv("HF_API_TOKEN", "your-default-token") # Replace with your actual token for fallback
|
8 |
|
9 |
# Function to call the Hugging Face Inference API
|
|
|
14 |
print(f"Request sent to: {API_URL}")
|
15 |
print(f"Payload: {payload}")
|
16 |
|
17 |
+
# Make the API call
|
18 |
response = requests.post(API_URL, headers=headers, json=payload)
|
19 |
|
20 |
print(f"Response Status Code: {response.status_code}")
|
|
|
22 |
if response.status_code == 200:
|
23 |
data = response.json()
|
24 |
print(f"Response Data: {data}")
|
25 |
+
# Assuming the model returns 'answer' and 'confidence'
|
26 |
return f"Question: {input_text}\nAnswer: {data.get('answer', 'No answer found.')}\nConfidence: {data.get('confidence', 'N/A')}"
|
27 |
else:
|
28 |
+
print(f"Error Response: {response.text}")
|
29 |
+
return f"Error: {response.status_code} - {response.text}"
|
|
|
|
|
30 |
|
31 |
except requests.exceptions.RequestException as e:
|
|
|
32 |
error_message = f"Network error during API call: {e}"
|
33 |
print(error_message)
|
34 |
return error_message
|
35 |
|
36 |
except ValueError as e:
|
|
|
37 |
error_message = f"Error parsing response JSON: {e}"
|
38 |
print(error_message)
|
39 |
return error_message
|
40 |
|
41 |
except KeyError as e:
|
|
|
42 |
error_message = f"KeyError: Missing expected key in response JSON: {e}"
|
43 |
print(error_message)
|
44 |
return error_message
|
45 |
|
46 |
except Exception as e:
|
|
|
47 |
error_message = f"Unexpected error during API call: {e}"
|
48 |
print(error_message)
|
49 |
return error_message
|
50 |
|
51 |
|
52 |
+
# Example of how you could set up specific queries about you (Rahul7star)
|
53 |
+
def ask_about_rahul7star(input_text):
|
54 |
+
# Example questions about your career, hobbies, and interests
|
55 |
+
predefined_answers = {
|
56 |
+
"Who is rahul7star?": "Rahul7star is a software developer and AI creator based in NSW. He is passionate about coding and AI.",
|
57 |
+
"What does Rahul7star do?": "Rahul7star works as a developer and enjoys solving complex coding problems. He loves traveling and exploring new destinations.",
|
58 |
+
"Tell me about Rahul7star's hobbies?": "Rahul7star enjoys driving scenic routes, having a cold beer after work, and traveling to new destinations, especially to places like Iceland to witness the Northern Lights.",
|
59 |
+
"What is Rahul7star known for?": "He is known for his work in AI, software development, and his ability to solve complex coding challenges."
|
60 |
+
}
|
61 |
+
|
62 |
+
# Check if the input matches any predefined question
|
63 |
+
if input_text in predefined_answers:
|
64 |
+
return predefined_answers[input_text]
|
65 |
+
else:
|
66 |
+
# If the question isn't predefined, call the Hugging Face model API
|
67 |
+
return call_huggingface_api(input_text)
|
68 |
+
|
69 |
+
|
70 |
+
# Gradio Interface for the AI agent
|
71 |
gr.Interface(
|
72 |
+
fn=ask_about_rahul7star,
|
73 |
inputs="text",
|
74 |
outputs="text",
|
75 |
examples=[
|
76 |
["Who is rahul7star?"],
|
77 |
["What does Rahul7star do?"],
|
78 |
+
["Tell me about Rahul7star's hobbies?"],
|
79 |
+
["What is Rahul7star known for?"]
|
80 |
],
|
81 |
title="Ask Rahul7star AI",
|
82 |
+
description="Ask questions about Rahul7star and get personalized answers powered by Hugging Face Inference API. Feel free to ask about his career, hobbies, or anything else."
|
83 |
).launch()
|