Nick088 commited on
Commit
672d648
1 Parent(s): cd247d6

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +115 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import numpy as np
4
+ import json
5
+ import re
6
+ import groq
7
+ from groq import Groq
8
+ import gradio as gr
9
+
10
+ # groq
11
+ client = Groq(api_key=os.environ.get("Groq_Api_Key"))
12
+
13
+ def handle_groq_error(e, model_name):
14
+ error_data = e.args[0]
15
+
16
+ if isinstance(error_data, str):
17
+ # Use regex to extract the JSON part of the string
18
+ json_match = re.search(r'(\{.*\})', error_data)
19
+ if json_match:
20
+ json_str = json_match.group(1)
21
+ # Ensure the JSON string is well-formed
22
+ json_str = json_str.replace("'", '"') # Replace single quotes with double quotes
23
+ error_data = json.loads(json_str)
24
+
25
+ if isinstance(e, groq.AuthenticationError):
26
+ if isinstance(error_data, dict) and 'error' in error_data and 'message' in error_data['error']:
27
+ error_message = error_data['error']['message']
28
+ raise gr.Error(error_message)
29
+ elif isinstance(e, groq.RateLimitError):
30
+ if isinstance(error_data, dict) and 'error' in error_data and 'message' in error_data['error']:
31
+ error_message = error_data['error']['message']
32
+ error_message = re.sub(r'org_[a-zA-Z0-9]+', 'org_(censored)', error_message) # censor org
33
+ raise gr.Error(error_message)
34
+ else:
35
+ raise gr.Error(f"Error during Groq API call: {e}")
36
+
37
+ # chat
38
+ def create_history_messages(history):
39
+ history_messages = []
40
+ for turn in history:
41
+ history_messages.append({"role": "user", "content": turn[0]})
42
+ if turn[1]: # Check if assistant's response is available
43
+ history_messages.append({"role": "assistant", "content": turn[1]})
44
+ return history_messages
45
+
46
+ MAX_SEED = np.iinfo(np.int32).max
47
+
48
+ def generate_initial_story():
49
+ initial_prompt = [{"role": "user", "content": "Create a short, spooky, and slightly comical story about being trapped in a haunted house. Describe the initial setting and the first challenge the character faces."}]
50
+ seed = random.randint(1, MAX_SEED)
51
+ try:
52
+ initial_completion = client.chat.completions.create(
53
+ messages=initial_prompt,
54
+ model="mixtral-8x7b-32768",
55
+ temperature=0.7,
56
+ max_tokens=1000,
57
+ top_p=0.5,
58
+ seed=seed
59
+ )
60
+ return initial_completion.choices[0].message.content
61
+ except groq.AuthenticationError as e:
62
+ handle_groq_error(e, model)
63
+ except groq.RateLimitError as e:
64
+ handle_groq_error(e, model)
65
+
66
+
67
+ def generate_response(prompt, history):
68
+ messages = create_history_messages(history)
69
+ messages.append({"role": "system", "content": "You are an Interactive Story Teller for an Halloween Spooky Escape Room Game! You are meant to generate a random story and let the user type their actions till they manage to find the exit."})
70
+ messages.append({"role": "user", "content": prompt})
71
+
72
+ seed = random.randint(1, MAX_SEED)
73
+
74
+ try:
75
+ stream = client.chat.completions.create(
76
+ messages=messages,
77
+ model="mixtral-8x7b-32768",
78
+ temperature=0.7,
79
+ max_tokens=32768,
80
+ top_p=0.5,
81
+ seed=seed,
82
+ stop=None,
83
+ stream=True,
84
+ )
85
+
86
+ response = ""
87
+ for chunk in stream:
88
+ delta_content = chunk.choices[0].delta.content
89
+ if delta_content is not None:
90
+ response += delta_content
91
+ yield response
92
+
93
+ print(messages)
94
+ except groq.AuthenticationError as e:
95
+ handle_groq_error(e, model)
96
+ except groq.RateLimitError as e:
97
+ handle_groq_error(e, model)
98
+
99
+ # Set initial chatbot history with AI-generated initial story
100
+ initial_message = generate_initial_story()
101
+ chatbot_history = [[None, initial_message]]
102
+
103
+ with gr.Blocks() as interface:
104
+ gr.Markdown("# 🎃Halloween Escape Room🎃")
105
+ gr.Markdown("Can you escape the haunted house? Type your actions to interact with the environment <br> Made by <a href='https://linktr.ee/nick088'>Nick088</a>.")
106
+
107
+ chatbot = gr.Chatbot(placeholder="<strong>Send 'start'</strong><br>")
108
+
109
+ gr.ChatInterface(
110
+ fn=generate_response,
111
+ type="tuples",
112
+ chatbot=chatbot
113
+ )
114
+
115
+ interface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ numpy==2.1.2
2
+ groq==0.11.0
3
+ gradio==5.4.0