awacke1 commited on
Commit
f02143a
ยท
verified ยท
1 Parent(s): de391e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -54
app.py CHANGED
@@ -13,17 +13,23 @@ import json
13
 
14
  warnings.filterwarnings('ignore')
15
 
16
- # Initialize client outside of reload block
17
- if gr.NO_RELOAD:
18
- ARXIV_CLIENT = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
 
 
 
 
 
19
 
20
  def generate_story(prompt, model_choice):
21
  """Generate story using specified model"""
22
  try:
23
- if ARXIV_CLIENT is None:
 
24
  return "Error: Story generation service is not available."
25
 
26
- result = ARXIV_CLIENT.predict(
27
  prompt=prompt,
28
  llm_model_picked=model_choice,
29
  stream_outputs=True,
@@ -50,7 +56,7 @@ def process_story_and_audio(prompt, model_choice):
50
  try:
51
  # Generate story
52
  story = generate_story(prompt, model_choice)
53
- if story.startswith("Error"):
54
  return story, None
55
 
56
  # Generate audio
@@ -60,57 +66,49 @@ def process_story_and_audio(prompt, model_choice):
60
  except Exception as e:
61
  return f"Error: {str(e)}", None
62
 
63
- # Define the Gradio interface
64
- def create_app():
65
- with gr.Blocks(title="AI Story Generator") as demo:
66
- gr.Markdown("""
67
- # ๐ŸŽญ AI Story Generator & Narrator
68
- Generate creative stories and listen to them!
69
- """)
70
-
71
- with gr.Row():
72
- with gr.Column():
73
- prompt_input = gr.Textbox(
74
- label="Story Concept",
75
- placeholder="Enter your story idea...",
76
- lines=3
77
- )
78
- model_choice = gr.Dropdown(
79
- label="Model",
80
- choices=[
81
- "mistralai/Mixtral-8x7B-Instruct-v0.1",
82
- "mistralai/Mistral-7B-Instruct-v0.2"
83
- ],
84
- value="mistralai/Mixtral-8x7B-Instruct-v0.1"
85
- )
86
- generate_btn = gr.Button("Generate Story")
87
-
88
- with gr.Row():
89
- story_output = gr.Textbox(
90
- label="Generated Story",
91
- lines=10,
92
- interactive=False
93
  )
94
-
95
- with gr.Row():
96
- audio_output = gr.Audio(
97
- label="Story Narration",
98
- type="filepath"
 
 
99
  )
100
-
101
- generate_btn.click(
102
- fn=process_story_and_audio,
103
- inputs=[prompt_input, model_choice],
104
- outputs=[story_output, audio_output]
 
 
 
 
 
 
 
 
105
  )
106
 
107
- return demo
 
 
 
 
108
 
109
- # Launch the app
110
  if __name__ == "__main__":
111
- demo = create_app()
112
- demo.launch(
113
- server_name="0.0.0.0",
114
- server_port=7860,
115
- share=True
116
- )
 
13
 
14
  warnings.filterwarnings('ignore')
15
 
16
+ # Initialize client outside of interface definition
17
+ arxiv_client = None
18
+
19
+ def init_client():
20
+ global arxiv_client
21
+ if arxiv_client is None:
22
+ arxiv_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
23
+ return arxiv_client
24
 
25
  def generate_story(prompt, model_choice):
26
  """Generate story using specified model"""
27
  try:
28
+ client = init_client()
29
+ if client is None:
30
  return "Error: Story generation service is not available."
31
 
32
+ result = client.predict(
33
  prompt=prompt,
34
  llm_model_picked=model_choice,
35
  stream_outputs=True,
 
56
  try:
57
  # Generate story
58
  story = generate_story(prompt, model_choice)
59
+ if isinstance(story, str) and story.startswith("Error"):
60
  return story, None
61
 
62
  # Generate audio
 
66
  except Exception as e:
67
  return f"Error: {str(e)}", None
68
 
69
+ # Create the Gradio interface
70
+ with gr.Blocks(title="AI Story Generator") as demo:
71
+ gr.Markdown("""
72
+ # ๐ŸŽญ AI Story Generator & Narrator
73
+ Generate creative stories and listen to them!
74
+ """)
75
+
76
+ with gr.Row():
77
+ with gr.Column():
78
+ prompt_input = gr.Textbox(
79
+ label="Story Concept",
80
+ placeholder="Enter your story idea...",
81
+ lines=3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  )
83
+ model_choice = gr.Dropdown(
84
+ label="Model",
85
+ choices=[
86
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
87
+ "mistralai/Mistral-7B-Instruct-v0.2"
88
+ ],
89
+ value="mistralai/Mixtral-8x7B-Instruct-v0.1"
90
  )
91
+ generate_btn = gr.Button("Generate Story")
92
+
93
+ with gr.Row():
94
+ story_output = gr.Textbox(
95
+ label="Generated Story",
96
+ lines=10,
97
+ interactive=False
98
+ )
99
+
100
+ with gr.Row():
101
+ audio_output = gr.Audio(
102
+ label="Story Narration",
103
+ type="filepath"
104
  )
105
 
106
+ generate_btn.click(
107
+ fn=process_story_and_audio,
108
+ inputs=[prompt_input, model_choice],
109
+ outputs=[story_output, audio_output]
110
+ )
111
 
112
+ # Launch the app using the current pattern
113
  if __name__ == "__main__":
114
+ demo.launch()