latterworks commited on
Commit
5d04ea6
1 Parent(s): c8bc787

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -48
app.py CHANGED
@@ -1,62 +1,46 @@
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()
 
1
  import gradio as gr
2
+ import requests
 
3
  from dotenv import load_dotenv
4
+ from telegram import Bot
5
 
6
  # Load environment variables
7
  load_dotenv()
8
+ TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
9
+ bot = Bot(token=TELEGRAM_BOT_TOKEN)
10
 
11
+ def send_message_to_user(chat_id, message):
12
+ """Send a test message to a user via the bot."""
13
+ try:
14
+ bot.send_message(chat_id=chat_id, text=message)
15
+ return f"Message sent to chat ID {chat_id}"
16
+ except Exception as e:
17
+ return f"Failed to send message: {str(e)}"
18
 
19
+ def check_bot_status():
20
+ """Check if the bot is online."""
21
  try:
22
+ response = bot.get_me()
23
+ return f"Bot is online: {response.username}"
 
 
 
 
 
 
 
24
  except Exception as e:
25
+ return f"Bot is offline: {str(e)}"
26
 
27
+ # Define the Gradio interface
28
+ with gr.Blocks() as dashboard:
29
+ gr.Markdown("# Telegram Bot Admin Dashboard")
30
 
31
+ # Bot status check
 
 
 
 
32
  with gr.Row():
33
+ bot_status_button = gr.Button("Check Bot Status")
34
+ bot_status_output = gr.Textbox(label="Bot Status")
35
+ bot_status_button.click(check_bot_status, inputs=None, outputs=bot_status_output)
36
+
37
+ # Send a message
 
 
38
  with gr.Row():
39
+ chat_id_input = gr.Textbox(label="Chat ID")
40
+ message_input = gr.Textbox(label="Message to Send")
41
+ send_message_button = gr.Button("Send Message")
42
+ send_message_output = gr.Textbox(label="Send Message Output")
43
+ send_message_button.click(send_message_to_user, inputs=[chat_id_input, message_input], outputs=send_message_output)
 
 
 
 
44
 
45
+ # Launch the dashboard
46
+ dashboard.launch()