mrbeliever commited on
Commit
b3eb52a
·
verified ·
1 Parent(s): 2221a1f

Update app.py

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