itsalissonsilva commited on
Commit
6940dc5
·
verified ·
1 Parent(s): f921119

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -1,9 +1,12 @@
1
  import gradio as gr
2
- import openai
3
  import os
 
4
 
5
- openai.api_key = os.getenv("OPENAI_API_KEY")
 
 
6
 
 
7
  initial_scenario = "You wake up in a strange forest. Paths lead in all directions.\n"
8
  choices = [
9
  "Walk north into the dense forest.",
@@ -12,34 +15,41 @@ choices = [
12
  "Move west where the land rises."
13
  ]
14
 
 
15
  history = [
16
- {"role": "system", "content": "You're an interactive text-based RPG engine. Continue the story based on the user's choices. Keep responses concise and vivid."},
 
 
 
 
 
 
17
  {"role": "user", "content": initial_scenario}
18
  ]
19
 
 
20
  def generate_continuation(choice):
21
- # Append player choice
22
  history.append({"role": "user", "content": f"> {choice}"})
23
 
24
- # Get OpenAI response
25
- response = openai.ChatCompletion.create(
26
- model="gpt-3.5-turbo", # or "gpt-4" if available
27
  messages=history,
28
  temperature=0.8,
29
  max_tokens=150,
30
  )
31
 
32
- continuation = response.choices[0].message["content"].strip()
33
  history.append({"role": "assistant", "content": continuation})
34
 
35
  # Build full story text
36
  story_text = initial_scenario + "\n"
37
- for msg in history[2:]: # Skip system and initial
38
  prefix = "You: " if msg["role"] == "user" else ""
39
  story_text += f"{prefix}{msg['content']}\n"
40
-
41
  return story_text.strip()
42
 
 
43
  with gr.Blocks() as app:
44
  with gr.Row():
45
  story_display = gr.Textbox(value=initial_scenario, lines=15, interactive=False, label="Story")
 
1
  import gradio as gr
 
2
  import os
3
+ from openai import OpenAI
4
 
5
+ # Initialize OpenAI client using API key from environment
6
+ api_key = os.getenv("OPENAI_API_KEY")
7
+ client = OpenAI(api_key=api_key)
8
 
9
+ # Initial game scenario and choices
10
  initial_scenario = "You wake up in a strange forest. Paths lead in all directions.\n"
11
  choices = [
12
  "Walk north into the dense forest.",
 
15
  "Move west where the land rises."
16
  ]
17
 
18
+ # Conversation history starts with a system prompt and the opening scene
19
  history = [
20
+ {
21
+ "role": "system",
22
+ "content": (
23
+ "You are a text-based RPG engine. Continue the story based on the player's choices. "
24
+ "Respond in immersive but concise prose. End each turn ready for the next input."
25
+ )
26
+ },
27
  {"role": "user", "content": initial_scenario}
28
  ]
29
 
30
+ # Function to generate story continuation from the OpenAI API
31
  def generate_continuation(choice):
 
32
  history.append({"role": "user", "content": f"> {choice}"})
33
 
34
+ response = client.chat.completions.create(
35
+ model="gpt-3.5-turbo", # You can switch to "gpt-4" if needed
 
36
  messages=history,
37
  temperature=0.8,
38
  max_tokens=150,
39
  )
40
 
41
+ continuation = response.choices[0].message.content.strip()
42
  history.append({"role": "assistant", "content": continuation})
43
 
44
  # Build full story text
45
  story_text = initial_scenario + "\n"
46
+ for msg in history[2:]: # skip system and initial
47
  prefix = "You: " if msg["role"] == "user" else ""
48
  story_text += f"{prefix}{msg['content']}\n"
49
+
50
  return story_text.strip()
51
 
52
+ # Gradio interface
53
  with gr.Blocks() as app:
54
  with gr.Row():
55
  story_display = gr.Textbox(value=initial_scenario, lines=15, interactive=False, label="Story")