Rathapoom commited on
Commit
e739911
·
verified ·
1 Parent(s): 49de2dc

Create APP.PY

Browse files
Files changed (1) hide show
  1. APP.PY +26 -0
APP.PY ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+
4
+ # ใส่ API Key
5
+ openai.api_key = OPENAI_API_KEY
6
+
7
+ def generate_response(prompt):
8
+ try:
9
+ response = openai.ChatCompletion.create(
10
+ model="gpt-4o-mini", # หรือ gpt-4 หากต้องการ
11
+ messages=[{"role": "user", "content": prompt}]
12
+ )
13
+ return response.choices[0].message["content"]
14
+ except Exception as e:
15
+ return f"Error: {str(e)}"
16
+
17
+ # สร้างอินเทอร์เฟซ Gradio
18
+ with gr.Blocks() as demo:
19
+ gr.Markdown("## Test OpenAI GPT API")
20
+ user_input = gr.Textbox(label="Enter your prompt")
21
+ output = gr.Textbox(label="GPT Response")
22
+ submit = gr.Button("Generate Response")
23
+ submit.click(generate_response, inputs=user_input, outputs=output)
24
+
25
+ if __name__ == "__main__":
26
+ demo.launch()