Spaces:
Running
Running
import os | |
import gradio as gr | |
import openai | |
# Saņemam API atslēgu no Hugging Face Secrets | |
api_key = os.getenv("OPENAI_API_KEY") | |
# Funkcija, kas nosūta jautājumu uz OpenAI | |
def ask_openai(jautajums): | |
if not api_key: | |
return "❌ API atslēga nav atrasta! Pievienojiet to Hugging Face Secrets." | |
try: | |
atbilde = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "user", "content": jautajums}], | |
max_tokens=200, | |
temperature=0.3 | |
) | |
return atbilde["choices"][0]["message"]["content"] | |
except Exception as e: | |
return f"⚠️ Kļūda: {str(e)}" | |
# Izveidojam Gradio saskarni | |
iface = gr.Interface(fn=ask_openai, inputs="text", outputs="text", title="🔌 Elektro AI Chat") | |
# Palaižam Gradio serveri | |
iface.launch(server_name="0.0.0.0", server_port=7860) | |