janushex commited on
Commit
2fed144
·
verified ·
1 Parent(s): e6daaa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -22
app.py CHANGED
@@ -1,38 +1,27 @@
1
- from flask import Flask, request, jsonify
2
- from flask_cors import CORS
3
- import openai
4
  import os
 
 
5
 
6
- app = Flask(__name__)
7
- CORS(app)
8
-
9
- # Saņemam OpenAI API atslēgu no Hugging Face Secrets
10
  api_key = os.getenv("OPENAI_API_KEY")
11
 
12
- @app.route("/ask", methods=["POST"])
13
- def ask_question():
14
- data = request.json
15
- user_question = data.get("question", "")
16
-
17
- if not user_question:
18
- return jsonify({"error": "Nav jautājuma!"}), 400
19
-
20
  response = openai.ChatCompletion.create(
21
  model="gpt-3.5-turbo",
22
- messages=[{"role": "user", "content": user_question}],
23
  max_tokens=200,
24
  temperature=0.3,
25
  api_key=api_key
26
  )
 
27
 
28
- return jsonify({"answer": response["choices"][0]["message"]["content"]})
 
29
 
30
- @app.route("/")
31
- def home():
32
- return "🚀 Flask API darbojas uz Hugging Face!"
33
 
34
- if __name__ == "__main__":
35
- app.run(host="0.0.0.0", port=7860)
36
 
37
 
38
 
 
 
 
 
1
  import os
2
+ import gradio as gr
3
+ import openai
4
 
5
+ # API atslēga 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(question):
 
 
 
 
 
 
10
  response = openai.ChatCompletion.create(
11
  model="gpt-3.5-turbo",
12
+ messages=[{"role": "user", "content": question}],
13
  max_tokens=200,
14
  temperature=0.3,
15
  api_key=api_key
16
  )
17
+ return response["choices"][0]["message"]["content"]
18
 
19
+ # Izveidojam Gradio lietotni
20
+ iface = gr.Interface(fn=ask_openai, inputs="text", outputs="text", title="Elektro AI Chat")
21
 
22
+ # Palaižam Gradio serveri
23
+ iface.launch(server_name="0.0.0.0", server_port=7860)
 
24
 
 
 
25
 
26
 
27