pokeberrypie commited on
Commit
74a0653
·
verified ·
1 Parent(s): dfffe01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -17
app.py CHANGED
@@ -1,23 +1,41 @@
1
- # app.py
2
-
3
  import gradio as gr
4
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
 
 
 
 
 
 
5
 
6
- def main():
7
- # Load the model and tokenizer
8
- model_name = "/tiiuae/falcon-180B" # Replace with the correct model name if different
9
- tokenizer = AutoTokenizer.from_pretrained(model_name)
10
- model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
 
 
11
 
12
- # Define the function that uses the model
13
- def generate_text(input_text):
14
- inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
15
- outputs = model.generate(**inputs, max_length=512, min_length=50, num_return_sequences=1)
16
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
17
 
18
- # Create and launch the Gradio interface
19
- iface = gr.Interface(fn=generate_text, inputs="text", outputs="text", title="Falcon 180B Text Generator")
20
- iface.launch()
 
 
 
 
 
21
 
 
22
  if __name__ == "__main__":
23
- main()
 
 
 
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()