Nicolás Larenas commited on
Commit
5ce3a86
·
verified ·
1 Parent(s): dfe1506

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -61
app.py CHANGED
@@ -1,65 +1,18 @@
1
- import google.generativeai as genai
2
- import os
3
- from config import (
4
- SYSTEM_MESSAGE,
5
- MODEL_NAME,
6
- DEFAULT_MAX_NEW_TOKENS,
7
- DEFAULT_TEMPERATURE,
8
- DEFAULT_TOP_P,
9
- DEFAULT_TOP_K,
10
- DEFAULT_STOP_SEQUENCES,
11
- )
12
 
13
- # Load Google API key from environment
14
- GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
15
- if not GOOGLE_API_KEY:
16
- raise ValueError("GOOGLE_API_KEY is not set. Please provide your API key.")
17
 
18
- genai.configure(api_key=GOOGLE_API_KEY)
 
 
19
 
20
- # Query AI model
21
- async def query_ai_model(
22
- message,
23
- history=None,
24
- system_message=SYSTEM_MESSAGE,
25
- max_new_tokens=DEFAULT_MAX_NEW_TOKENS,
26
- temperature=DEFAULT_TEMPERATURE,
27
- top_p=DEFAULT_TOP_P,
28
- top_k=DEFAULT_TOP_K,
29
- stop_sequences=DEFAULT_STOP_SEQUENCES,
30
- ):
31
- try:
32
- # Build the conversation history in the required format
33
- messages = []
34
 
35
- if system_message:
36
- messages.append({'role': 'system', 'content': system_message})
37
-
38
- if history:
39
- for user_msg, bot_reply in history:
40
- if user_msg:
41
- messages.append({'role': 'user', 'content': user_msg})
42
- if bot_reply:
43
- messages.append({'role': 'assistant', 'content': bot_reply})
44
-
45
- # Append the new user message
46
- messages.append({'role': 'user', 'content': message})
47
-
48
- # Set parameters
49
- parameters = {
50
- 'temperature': temperature,
51
- 'top_p': top_p,
52
- 'top_k': top_k,
53
- 'max_output_tokens': int(max_new_tokens),
54
- 'stop_sequences': stop_sequences,
55
- }
56
-
57
- # Generate response
58
- response = genai.generate_chat(
59
- model=MODEL_NAME,
60
- messages=messages,
61
- **parameters
62
- )
63
- return response.candidates[0]['content']
64
- except Exception as e:
65
- return f"An error occurred: {str(e)}"
 
1
+ import asyncio
2
+ import nest_asyncio
3
+ from gradio_interface import create_gradio_interface
4
+ from discord_bot import run_discord_bot
 
 
 
 
 
 
 
5
 
6
+ # Apply nest_asyncio to allow nested event loops
7
+ nest_asyncio.apply()
 
 
8
 
9
+ if __name__ == "__main__":
10
+ # Create the event loop
11
+ loop = asyncio.get_event_loop()
12
 
13
+ # Start the Discord bot in the event loop
14
+ loop.create_task(run_discord_bot())
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Create and launch the Gradio interface
17
+ demo = create_gradio_interface()
18
+ demo.launch(share=True)