latterworks commited on
Commit
61f65e4
1 Parent(s): 22b5803

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -3
app.py CHANGED
@@ -1,7 +1,62 @@
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()
 
1
  import gradio as gr
2
+ import os
3
+ import openai
4
+ from dotenv import load_dotenv
5
 
6
+ # Load environment variables
7
+ load_dotenv()
8
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
9
+
10
+ # Initialize OpenAI client
11
+ openai.api_key = OPENAI_API_KEY
12
+ openai.api_base = "https://api.aimlapi.com/"
13
+
14
+ # Greeting function for Gradio
15
  def greet(name):
16
+ return f"Hello {name}!"
17
+
18
+ # OpenAI Chat Completion Function
19
+ def get_openai_response(user_content, model="o1-preview", max_tokens=500):
20
+ try:
21
+ chat_completion = openai.ChatCompletion.create(
22
+ model=model,
23
+ messages=[
24
+ {"role": "user", "content": user_content}
25
+ ],
26
+ max_tokens=max_tokens
27
+ )
28
+ response = chat_completion.choices[0].message['content']
29
+ return response
30
+ except Exception as e:
31
+ return f"An error occurred: {e}"
32
+
33
+ # Gradio interface
34
+ def openai_chat_interface(user_content, model, max_tokens):
35
+ return get_openai_response(user_content, model, max_tokens)
36
+
37
+ # Define Gradio Interface
38
+ with gr.Blocks() as demo:
39
+ gr.Markdown("# OpenAI Chat Completion & Greeting Interface")
40
+
41
+ # Greeting section
42
+ with gr.Row():
43
+ gr.Markdown("### Greeting Section")
44
+ greet_input = gr.Textbox(label="Enter your name")
45
+ greet_output = gr.Textbox(label="Greeting Output")
46
+ greet_button = gr.Button("Greet")
47
+ greet_button.click(greet, inputs=greet_input, outputs=greet_output)
48
+
49
+ # OpenAI Chat Completion Section
50
+ with gr.Row():
51
+ gr.Markdown("### OpenAI Chat Completion")
52
+ user_input = gr.Textbox(label="Enter your message")
53
+ model_input = gr.Radio(["o1-preview", "o1-mini"], label="Model Selection", value="o1-preview")
54
+ max_tokens_input = gr.Slider(1, 7000, value=500, label="Max Tokens")
55
+ openai_response = gr.Textbox(label="OpenAI Response")
56
+ chat_button = gr.Button("Get OpenAI Response")
57
+ chat_button.click(openai_chat_interface,
58
+ inputs=[user_input, model_input, max_tokens_input],
59
+ outputs=openai_response)
60
 
61
+ # Launch Gradio app
62
+ demo.launch()