Spaces:
Sleeping
Sleeping
File size: 4,204 Bytes
672d648 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
import os
import random
import numpy as np
import json
import re
import groq
from groq import Groq
import gradio as gr
# groq
client = Groq(api_key=os.environ.get("Groq_Api_Key"))
def handle_groq_error(e, model_name):
error_data = e.args[0]
if isinstance(error_data, str):
# Use regex to extract the JSON part of the string
json_match = re.search(r'(\{.*\})', error_data)
if json_match:
json_str = json_match.group(1)
# Ensure the JSON string is well-formed
json_str = json_str.replace("'", '"') # Replace single quotes with double quotes
error_data = json.loads(json_str)
if isinstance(e, groq.AuthenticationError):
if isinstance(error_data, dict) and 'error' in error_data and 'message' in error_data['error']:
error_message = error_data['error']['message']
raise gr.Error(error_message)
elif isinstance(e, groq.RateLimitError):
if isinstance(error_data, dict) and 'error' in error_data and 'message' in error_data['error']:
error_message = error_data['error']['message']
error_message = re.sub(r'org_[a-zA-Z0-9]+', 'org_(censored)', error_message) # censor org
raise gr.Error(error_message)
else:
raise gr.Error(f"Error during Groq API call: {e}")
# chat
def create_history_messages(history):
history_messages = []
for turn in history:
history_messages.append({"role": "user", "content": turn[0]})
if turn[1]: # Check if assistant's response is available
history_messages.append({"role": "assistant", "content": turn[1]})
return history_messages
MAX_SEED = np.iinfo(np.int32).max
def generate_initial_story():
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."}]
seed = random.randint(1, MAX_SEED)
try:
initial_completion = client.chat.completions.create(
messages=initial_prompt,
model="mixtral-8x7b-32768",
temperature=0.7,
max_tokens=1000,
top_p=0.5,
seed=seed
)
return initial_completion.choices[0].message.content
except groq.AuthenticationError as e:
handle_groq_error(e, model)
except groq.RateLimitError as e:
handle_groq_error(e, model)
def generate_response(prompt, history):
messages = create_history_messages(history)
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."})
messages.append({"role": "user", "content": prompt})
seed = random.randint(1, MAX_SEED)
try:
stream = client.chat.completions.create(
messages=messages,
model="mixtral-8x7b-32768",
temperature=0.7,
max_tokens=32768,
top_p=0.5,
seed=seed,
stop=None,
stream=True,
)
response = ""
for chunk in stream:
delta_content = chunk.choices[0].delta.content
if delta_content is not None:
response += delta_content
yield response
print(messages)
except groq.AuthenticationError as e:
handle_groq_error(e, model)
except groq.RateLimitError as e:
handle_groq_error(e, model)
# Set initial chatbot history with AI-generated initial story
initial_message = generate_initial_story()
chatbot_history = [[None, initial_message]]
with gr.Blocks() as interface:
gr.Markdown("# 🎃Halloween Escape Room🎃")
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>.")
chatbot = gr.Chatbot(placeholder="<strong>Send 'start'</strong><br>")
gr.ChatInterface(
fn=generate_response,
type="tuples",
chatbot=chatbot
)
interface.launch(share=True) |