Patrick079 commited on
Commit
fa9515b
·
verified ·
1 Parent(s): a972f9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -53
app.py CHANGED
@@ -1,71 +1,54 @@
1
  import random
2
- import gradio as gr
3
- import ollama
4
- from huggingface_hub import InferenceClient
5
  import os
 
 
6
 
7
 
8
  def chat_short_story(length, genre, theme, tone, writing_style):
9
  """
10
- Generates a creative short story using a Hugging Face model.
11
  Parameters:
12
- length (int): The approximate word count for the story.
13
- genre (str): The genre of the story (e.g., fantasy, mystery).
14
- theme (str): The central theme of the story.
15
- tone (str): The tone of the story (e.g., humorous, serious).
16
- writing_style (str): The writing style (e.g., poetic, conversational).
17
  Returns:
18
  str: The generated short story, or an error message if unsuccessful.
19
  """
20
- # System message to define the assistant's role
21
- system_message = (
22
- "You are a highly creative short story writer capable of crafting stories across any genre. "
23
- "For every story created, ensure you generate a suitable title."
24
- )
25
 
26
- # Construct the user prompt
 
 
 
 
 
 
27
  prompt = (
28
  f"Write a creative short story of approximately {length} words in the {genre} genre. "
29
  f"Use a {writing_style} writing style with a {tone} tone. "
30
  f"The story should revolve around the theme of {theme}. "
31
- f"Ensure the narrative is compelling and includes a suitable title."
32
  )
33
 
34
- # Retrieve the Hugging Face API key from the environment
35
- HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
36
- if not HUGGINGFACE_API_KEY:
37
- return "Error: Hugging Face API key not found. Please set the HUGGINGFACE_API_KEY environment variable."
38
-
39
- # Initialize the Hugging Face Inference Client
40
  try:
41
- client = InferenceClient(api_key=HUGGINGFACE_API_KEY)
 
 
 
 
42
  except Exception as e:
43
- return f"Error: Failed to initialize Hugging Face client. Details: {e}"
44
-
45
- # Interact with the model
46
- try:
47
- result = client.chat.completions.create(
48
- model="meta-llama/Llama-3.2-3B-Instruct",
49
- messages=[
50
- {"role": "system", "content": system_message},
51
- {"role": "user", "content": prompt},
52
- ]
53
- )
54
- except Exception as e:
55
- return f"Error: Failed to interact with the model. Details: {e}"
56
-
57
- # Extract the story content from the response
58
- if "choices" in result and len(result["choices"]) > 0:
59
- return result["choices"][0]["message"]["content"]
60
- else:
61
- return "Error: No story generated. Please check your prompt or model configuration."
62
-
63
 
64
 
65
  # Predefined options
66
  Length = [100, 250, 750]
67
- Length = [l for l in Length if l >= 100]
68
- r_length = random.choice(Length)
69
 
70
  Genre = [
71
  "Fiction", "Nonfiction", "Drama", "Poetry", "Fantasy", "Horror", "Mystery",
@@ -86,19 +69,19 @@ r_Style = random.choice(Writing_Styles)
86
  Tones = ["Formal", "Optimistic", "Worried", "Friendly", "Curious", "Assertive", "Encouraging"]
87
  r_tones = random.choice(Tones)
88
 
89
-
90
  # Gradio Interface setup
91
  iface = gr.Interface(
92
  fn=chat_short_story,
93
  inputs=[
94
- gr.Slider(value=100, label="Story_Length", minimum=100, maximum=2500),
95
- gr.Dropdown(label="Story_Genre", choices=Genre),
96
- gr.Dropdown(label="Story_Theme", choices=Themes),
97
- gr.Dropdown(label="Writing_Styles", choices=Writing_Styles),
98
- gr.Dropdown(label="Story_Tone", choices=Tones)
99
  ],
100
- outputs=gr.Text(),
101
- title="Welcome to the Patrick's Story Generator"
 
102
  )
103
 
104
  iface.launch()
 
1
  import random
 
 
 
2
  import os
3
+ import gradio as gr
4
+ import google.generativeai as genai # Importing the Gemini API module
5
 
6
 
7
  def chat_short_story(length, genre, theme, tone, writing_style):
8
  """
9
+ Generates a creative short story using Gemini API.
10
  Parameters:
11
+ length (int): Approximate word count for the story.
12
+ genre (str): Genre of the story.
13
+ theme (str): Central theme of the story.
14
+ tone (str): Tone of the story.
15
+ writing_style (str): Writing style for the story.
16
  Returns:
17
  str: The generated short story, or an error message if unsuccessful.
18
  """
19
+ # Retrieve the Gemini API key from the environment
20
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
21
+ if not GEMINI_API_KEY:
22
+ return "Error: Gemini API key not found. Please set the GEMINI_API_KEY environment variable."
 
23
 
24
+ # Configure the Gemini API
25
+ try:
26
+ genai.configure(api_key=GEMINI_API_KEY)
27
+ except Exception as e:
28
+ return f"Error: Failed to configure the Gemini API. Details: {e}"
29
+
30
+ # Construct the prompt
31
  prompt = (
32
  f"Write a creative short story of approximately {length} words in the {genre} genre. "
33
  f"Use a {writing_style} writing style with a {tone} tone. "
34
  f"The story should revolve around the theme of {theme}. "
35
+ f"Ensure it is engaging and includes a suitable title."
36
  )
37
 
38
+ # Generate a response using the Gemini model
 
 
 
 
 
39
  try:
40
+ response = genai.generate_text(prompt=prompt, model="gemini-1.5-flash")
41
+ if "candidates" in response and len(response["candidates"]) > 0:
42
+ return response["candidates"][0]["output"] # Extract the story content
43
+ else:
44
+ return "Error: No story generated. Please check your input or try again."
45
  except Exception as e:
46
+ return f"Error: Failed to generate story. Details: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
 
49
  # Predefined options
50
  Length = [100, 250, 750]
51
+ r_length = random.choice([l for l in Length if l >= 100])
 
52
 
53
  Genre = [
54
  "Fiction", "Nonfiction", "Drama", "Poetry", "Fantasy", "Horror", "Mystery",
 
69
  Tones = ["Formal", "Optimistic", "Worried", "Friendly", "Curious", "Assertive", "Encouraging"]
70
  r_tones = random.choice(Tones)
71
 
 
72
  # Gradio Interface setup
73
  iface = gr.Interface(
74
  fn=chat_short_story,
75
  inputs=[
76
+ gr.Slider(value=100, label="Story Length", minimum=100, maximum=2500, step=50),
77
+ gr.Dropdown(label="Story Genre", choices=Genre),
78
+ gr.Dropdown(label="Story Theme", choices=Themes),
79
+ gr.Dropdown(label="Writing Style", choices=Writing_Styles),
80
+ gr.Dropdown(label="Story Tone", choices=Tones)
81
  ],
82
+ outputs=gr.Textbox(label="Generated Story"),
83
+ title="Patrick's Story Generator",
84
+ description="Generate creative short stories tailored to your preferences."
85
  )
86
 
87
  iface.launch()