Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,22 +2,27 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import openai
|
4 |
|
5 |
-
# API
|
6 |
api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
# Funkcija, kas nosūta jautājumu uz OpenAI
|
9 |
-
def ask_openai(
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Palaižam Gradio serveri
|
23 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
@@ -25,4 +30,3 @@ iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
25 |
|
26 |
|
27 |
|
28 |
-
|
|
|
2 |
import gradio as gr
|
3 |
import openai
|
4 |
|
5 |
+
# Saņemam API atslēgu no Hugging Face Secrets
|
6 |
api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
# Funkcija, kas nosūta jautājumu uz OpenAI
|
9 |
+
def ask_openai(jautajums):
|
10 |
+
if not api_key:
|
11 |
+
return "❌ API atslēga nav atrasta! Pievienojiet to Hugging Face Secrets."
|
12 |
+
|
13 |
+
try:
|
14 |
+
atbilde = openai.ChatCompletion.create(
|
15 |
+
model="gpt-3.5-turbo",
|
16 |
+
messages=[{"role": "user", "content": jautajums}],
|
17 |
+
max_tokens=200,
|
18 |
+
temperature=0.3
|
19 |
+
)
|
20 |
+
return atbilde["choices"][0]["message"]["content"]
|
21 |
+
except Exception as e:
|
22 |
+
return f"⚠️ Kļūda: {str(e)}"
|
23 |
+
|
24 |
+
# Izveidojam Gradio saskarni
|
25 |
+
iface = gr.Interface(fn=ask_openai, inputs="text", outputs="text", title="🔌 Elektro AI Chat")
|
26 |
|
27 |
# Palaižam Gradio serveri
|
28 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
30 |
|
31 |
|
32 |
|
|