Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,51 +2,55 @@ import streamlit as st
|
|
2 |
import os
|
3 |
from openai import OpenAI
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
from openai import OpenAI
|
4 |
|
5 |
+
# Set the API key
|
6 |
+
API_KEY = os.getenv("NEBIUS_API_KEY") # Use the secret key set in Hugging Face or locally
|
7 |
+
if not API_KEY:
|
8 |
+
st.error("API Key is not set. Please set the NEBIUS_API_KEY environment variable.")
|
9 |
+
else:
|
10 |
+
# Initialize the OpenAI client
|
11 |
+
client = OpenAI(
|
12 |
+
base_url="https://api.studio.nebius.ai/v1/",
|
13 |
+
api_key=API_KEY
|
14 |
+
)
|
15 |
+
|
16 |
+
# Streamlit app title
|
17 |
+
st.title("AI Title Generator")
|
18 |
+
|
19 |
+
# Text Area Input Bar
|
20 |
+
user_input = st.text_area(
|
21 |
+
label="Enter a description for generating titles:",
|
22 |
+
placeholder="e.g., Man who went to jail for no reason"
|
23 |
+
)
|
24 |
+
|
25 |
+
# Generate Button
|
26 |
+
if st.button("Generate Titles"):
|
27 |
+
if user_input.strip():
|
28 |
+
try:
|
29 |
+
# API call to OpenAI
|
30 |
+
completion = client.chat.completions.create(
|
31 |
+
model="nvidia/Llama-3.1-Nemotron-70B-Instruct-HF",
|
32 |
+
messages=[
|
33 |
+
{"role": "system", "content": "Your task is to generate 3 very short titles based on the user input."},
|
34 |
+
{"role": "user", "content": user_input}
|
35 |
+
],
|
36 |
+
temperature=0.6,
|
37 |
+
maxTokens=512,
|
38 |
+
topP=0.9,
|
39 |
+
topK=50
|
40 |
+
)
|
41 |
+
|
42 |
+
# Extracting the generated titles
|
43 |
+
response_content = completion.choices[0].message["content"]
|
44 |
+
|
45 |
+
# Output Text Area
|
46 |
+
st.text_area(
|
47 |
+
label="Generated Titles:",
|
48 |
+
value=response_content,
|
49 |
+
height=200,
|
50 |
+
disabled=True
|
51 |
+
)
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
st.error(f"An error occurred: {e}")
|
55 |
+
else:
|
56 |
+
st.warning("Please provide input before clicking Generate.")
|