cannin commited on
Commit
399f8c1
·
1 Parent(s): 5216c03

Initial import (not demo app.py)

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +27 -4
  3. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ del.py
app.py CHANGED
@@ -1,8 +1,31 @@
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
  import gradio as gr
3
 
 
 
4
 
5
+ def chat_with_openai(api_key, prompt):
6
+ if not api_key.strip() or not prompt.strip():
7
+ return "Please provide both an API key and a prompt."
8
 
9
+ try:
10
+ openai.api_key = api_key
11
+ response = openai.ChatCompletion.create(
12
+ model="gpt-4o-mini",
13
+ messages=[{"role": "user", "content": prompt}],
14
+ )
15
+ return response["choices"][0]["message"]["content"]
16
+ except Exception as e:
17
+ return f"Error: {str(e)}"
18
+
19
+
20
+ iface = gr.Interface(
21
+ fn=chat_with_openai,
22
+ inputs=[
23
+ gr.Textbox(label="OpenAI API Key", placeholder="sk-...", type="password"),
24
+ gr.Textbox(label="Prompt", placeholder="Tell me a joke"),
25
+ ],
26
+ outputs="text",
27
+ title="OpenAI Example",
28
+ description="Enter your OpenAI API key and a prompt like 'Tell me a joke'."
29
+ )
30
+
31
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai>=1.63.1
2
+ gradio>=5.23.2