Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
# from huggingface_hub import InferenceClient
|
| 3 |
from transformers import pipeline, AutoTokenizer
|
| 4 |
import os
|
| 5 |
|
|
@@ -9,23 +8,19 @@ hf_token = os.getenv("HF_TOKEN")
|
|
| 9 |
if not hf_token:
|
| 10 |
raise ValueError("API token is not set. Please set the HF_TOKEN environment variable in Space Settings.")
|
| 11 |
|
| 12 |
-
"""
|
| 13 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 14 |
-
"""
|
| 15 |
-
# requires space hardware update to use large models (TODO)
|
| 16 |
-
# client = InferenceClient("mistralai/Mistral-Large-Instruct-2407")
|
| 17 |
-
# Note change in instantiation***
|
| 18 |
-
# pipeline move to func
|
| 19 |
-
# text_generator = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct", use_auth_token=hf_token, trust_remote_code=True)
|
| 20 |
-
|
| 21 |
def authenticate_and_generate(message, history, system_message, max_tokens, temperature, top_p):
|
| 22 |
try:
|
| 23 |
# Initialize the text-generation pipeline with the provided token
|
| 24 |
text_generator = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct", use_auth_token=hf_token, trust_remote_code=True)
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
if text_generator.tokenizer is None:
|
| 28 |
-
raise RuntimeError("
|
| 29 |
|
| 30 |
# Ensure that system_message is a string
|
| 31 |
system_message = str(system_message)
|
|
@@ -47,38 +42,30 @@ def authenticate_and_generate(message, history, system_message, max_tokens, temp
|
|
| 47 |
except Exception as e:
|
| 48 |
return str(e) # Return the error message for debugging
|
| 49 |
|
| 50 |
-
"""
|
| 51 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 52 |
-
"""
|
| 53 |
athena = gr.ChatInterface(
|
| 54 |
fn=authenticate_and_generate,
|
| 55 |
additional_inputs=[
|
| 56 |
-
gr.Textbox(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"),
|
| 72 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 73 |
-
gr.Slider(
|
| 74 |
-
minimum=0.1,
|
| 75 |
-
maximum=1.0,
|
| 76 |
-
value=0.95,
|
| 77 |
-
step=0.05,
|
| 78 |
-
label="Top-p (nucleus sampling)",
|
| 79 |
-
),
|
| 80 |
],
|
| 81 |
)
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
-
athena.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import pipeline, AutoTokenizer
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 8 |
if not hf_token:
|
| 9 |
raise ValueError("API token is not set. Please set the HF_TOKEN environment variable in Space Settings.")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def authenticate_and_generate(message, history, system_message, max_tokens, temperature, top_p):
|
| 12 |
try:
|
| 13 |
# Initialize the text-generation pipeline with the provided token
|
| 14 |
text_generator = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct", use_auth_token=hf_token, trust_remote_code=True)
|
| 15 |
+
|
| 16 |
+
# Load the tokenizer separately if needed
|
| 17 |
+
try:
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct", use_auth_token=hf_token)
|
| 19 |
+
except Exception as e:
|
| 20 |
+
raise RuntimeError(f"Failed to load the tokenizer: {str(e)}")
|
| 21 |
|
| 22 |
if text_generator.tokenizer is None:
|
| 23 |
+
raise RuntimeError("The tokenizer is not available. Check the model and API token.")
|
| 24 |
|
| 25 |
# Ensure that system_message is a string
|
| 26 |
system_message = str(system_message)
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
return str(e) # Return the error message for debugging
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
athena = gr.ChatInterface(
|
| 46 |
fn=authenticate_and_generate,
|
| 47 |
additional_inputs=[
|
| 48 |
+
gr.Textbox(
|
| 49 |
+
value="""
|
| 50 |
+
You are a marketing-minded content writer for Plan.com (a UK telecommunications company).
|
| 51 |
+
You will be provided a bullet-point list of guidelines from which to generate an article to be published in the company News section of the website.
|
| 52 |
+
Please follow these guidelines:
|
| 53 |
+
- Always speak using British English expressions, syntax, and spelling.
|
| 54 |
+
- Make the articles engaging and fun, but also professional and informative.
|
| 55 |
+
To provide relevant contextual information about the company, please source information from the following websites:
|
| 56 |
+
- https://plan.com/our-story
|
| 57 |
+
- https://plan.com/products-services
|
| 58 |
+
- https://plan.com/features/productivity-and-performance
|
| 59 |
+
- https://plan.com/features/security-and-connectivity
|
| 60 |
+
- https://plan.com/features/connectivity-and-cost
|
| 61 |
+
""",
|
| 62 |
+
label="System message"
|
| 63 |
+
),
|
| 64 |
gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"),
|
| 65 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 66 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
],
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
athena.launch()
|