Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,41 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import gradio as gr
|
4 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
21 |
|
|
|
22 |
if __name__ == "__main__":
|
23 |
-
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Fetch the API key from environment variables, securely set in Space's settings
|
6 |
+
api_key = os.getenv("HF_API_KEY")
|
7 |
+
|
8 |
+
if api_key is not None:
|
9 |
+
os.environ["HF_HOME"] = "/workspace" # Hugging Face Spaces provides a /workspace directory
|
10 |
+
os.environ["TRANSFORMERS_CACHE"] = "/workspace/cache" # Cache directory in Space
|
11 |
+
os.environ["HF_API_KEY"] = api_key
|
12 |
|
13 |
+
# Attempt to load your model using the pipeline API
|
14 |
+
model_name = "tiiuae/falcon-180B" # Ensure this is the correct model name
|
15 |
+
try:
|
16 |
+
nlp_model = pipeline('text-generation', model=model_name)
|
17 |
+
except Exception as e:
|
18 |
+
nlp_model = None # fallback in case of an error
|
19 |
+
else:
|
20 |
+
nlp_model = None
|
21 |
+
print("HF_API_KEY not found. Please set it in the Space's secrets.")
|
22 |
|
23 |
+
def generate_text(prompt):
|
24 |
+
if nlp_model:
|
25 |
+
response = nlp_model(prompt, max_length=50, num_return_sequences=1)
|
26 |
+
return response[0]['generated_text']
|
27 |
+
else:
|
28 |
+
return "Model could not be loaded. Please check the model name and API key."
|
29 |
|
30 |
+
# Gradio interface
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=generate_text,
|
33 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your text here..."),
|
34 |
+
outputs="text",
|
35 |
+
title="Text Generation with Falcon Model",
|
36 |
+
description="Enter some text and see how Falcon model continues it!"
|
37 |
+
)
|
38 |
|
39 |
+
# The following line is essential for Hugging Face Spaces
|
40 |
if __name__ == "__main__":
|
41 |
+
iface.launch()
|