Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,62 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import os
|
3 |
-
from openai import OpenAI
|
4 |
|
5 |
-
#
|
6 |
-
API_KEY = os.
|
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/chat/completions",
|
13 |
-
api_key=API_KEY
|
14 |
-
)
|
15 |
|
16 |
-
|
17 |
-
st.
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
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 |
-
max_tokens=512, # Corrected argument name
|
38 |
-
top_p=0.9,
|
39 |
-
top_k=50
|
40 |
-
)
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
44 |
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
st.text_area(
|
47 |
label="Generated Titles:",
|
48 |
-
value=
|
49 |
height=200,
|
50 |
disabled=True
|
51 |
)
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
st.warning("Please provide input before clicking Generate.")
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
3 |
import os
|
|
|
4 |
|
5 |
+
# API key from environment variable
|
6 |
+
API_KEY = os.environ.get("NEBIUS_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
if not API_KEY:
|
9 |
+
st.error("API key not found. Please set the `NEBIUS_API_KEY` environment variable.")
|
10 |
|
11 |
+
# Function to call Nebius API
|
12 |
+
def generate_response(prompt, api_key):
|
13 |
+
api_url = "https://api.studio.nebius.ai/v1/chat/completions"
|
14 |
+
headers = {"Authorization": f"Bearer {api_key}"}
|
15 |
+
payload = {
|
16 |
+
"model": "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF",
|
17 |
+
"messages": [
|
18 |
+
{"role": "system", "content": "You are an AI that generates short, creative titles based on the user's input."},
|
19 |
+
{"role": "user", "content": prompt}
|
20 |
+
],
|
21 |
+
"temperature": 0.6,
|
22 |
+
"max_tokens": 512,
|
23 |
+
"top_p": 0.9,
|
24 |
+
"top_k": 50
|
25 |
+
}
|
26 |
+
|
27 |
+
# API request
|
28 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
29 |
+
|
30 |
+
if response.status_code == 200:
|
31 |
+
return response.json()
|
32 |
+
else:
|
33 |
+
st.error(f"Error: {response.status_code}, {response.text}")
|
34 |
+
return None
|
35 |
|
36 |
+
# Streamlit App
|
37 |
+
st.title("AI Title Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Input bar for user prompt
|
40 |
+
user_input = st.text_area(
|
41 |
+
label="Enter a description for generating titles:",
|
42 |
+
placeholder="e.g., Man who went to jail for no reason"
|
43 |
+
)
|
44 |
|
45 |
+
# Generate button
|
46 |
+
if st.button("Generate Titles"):
|
47 |
+
if user_input.strip():
|
48 |
+
result = generate_response(user_input, API_KEY)
|
49 |
+
if result:
|
50 |
+
try:
|
51 |
+
# Extracting generated titles
|
52 |
+
assistant_message = result["choices"][0]["message"]["content"]
|
53 |
st.text_area(
|
54 |
label="Generated Titles:",
|
55 |
+
value=assistant_message,
|
56 |
height=200,
|
57 |
disabled=True
|
58 |
)
|
59 |
+
except KeyError as e:
|
60 |
+
st.error(f"Unexpected response format: {e}")
|
61 |
+
else:
|
62 |
+
st.warning("Please provide input before clicking Generate.")
|
|