garvitcpp commited on
Commit
a1a8a85
·
verified ·
1 Parent(s): b5a39dc

Update app/services/story_refinement.py

Browse files
Files changed (1) hide show
  1. app/services/story_refinement.py +52 -52
app/services/story_refinement.py CHANGED
@@ -1,53 +1,53 @@
1
- import re
2
- import unicodedata
3
- import google.generativeai as genai
4
- from fastapi import HTTPException
5
- from app.core.config import settings
6
-
7
- # Configure Gemini API
8
- genai.configure(api_key=settings.GOOGLE_API_KEY)
9
- gemini_model = genai.GenerativeModel('gemini-1.5-flash')
10
-
11
- def clean_text(story: str) -> str:
12
- """Clean and normalize the generated text"""
13
- cleaned_story = unicodedata.normalize("NFKD", story)
14
- cleaned_story = re.sub(r'[\n\\]+', ' ', cleaned_story)
15
- cleaned_story = re.sub(r'\s+', ' ', cleaned_story)
16
- cleaned_story = (
17
- cleaned_story.replace("“", "\"").replace("â€", "\"").replace("’", "'")
18
- .replace("â€TMs", "'s").replace("“", "\"").replace("Ó", "O")
19
- .replace("â", "").replace("�", "")
20
- )
21
- cleaned_story = cleaned_story.replace("\\", "")
22
- return cleaned_story.strip()
23
-
24
- async def refine_story(story: str) -> str:
25
- """Refine the story using the Gemini API with two-step refinement"""
26
- try:
27
- # First refinement
28
- first_prompt = f"""
29
- Refine the following story to make it coherent, grammatically correct, and fluent.
30
- Maintain the original plot and ending, but enhance readability, fluency, and consistency:
31
-
32
- {story}
33
- """
34
-
35
- first_response = gemini_model.generate_content(first_prompt)
36
- first_refined = first_response.text if hasattr(first_response, 'text') else str(first_response)
37
-
38
- # Second refinement for final polishing
39
- final_prompt = f"""
40
- Please perform a final polish on this story to ensure perfect coherence,
41
- flow, and narrative structure while maintaining the essence of the story:
42
-
43
- {first_refined}
44
- """
45
-
46
- final_response = gemini_model.generate_content(final_prompt)
47
- final_refined = final_response.text if hasattr(final_response, 'text') else str(final_response)
48
-
49
- return final_refined if final_refined else "No refined story generated."
50
-
51
- except Exception as e:
52
- print(f"Error in story refinement: {str(e)}")
53
  return story # Return original story if refinement fails
 
1
+ import re
2
+ import unicodedata
3
+ import google.generativeai as genai
4
+ from fastapi import HTTPException
5
+ from app.core.config import settings
6
+
7
+ # Configure Gemini API
8
+ genai.configure(api_key=settings.GOOGLE_API_KEY)
9
+ gemini_model = genai.GenerativeModel('gemini-2.0')
10
+
11
+ def clean_text(story: str) -> str:
12
+ """Clean and normalize the generated text"""
13
+ cleaned_story = unicodedata.normalize("NFKD", story)
14
+ cleaned_story = re.sub(r'[\n\\]+', ' ', cleaned_story)
15
+ cleaned_story = re.sub(r'\s+', ' ', cleaned_story)
16
+ cleaned_story = (
17
+ cleaned_story.replace("“", "\"").replace("â€", "\"").replace("’", "'")
18
+ .replace("â€TMs", "'s").replace("“", "\"").replace("Ó", "O")
19
+ .replace("â", "").replace("�", "")
20
+ )
21
+ cleaned_story = cleaned_story.replace("\\", "")
22
+ return cleaned_story.strip()
23
+
24
+ async def refine_story(story: str) -> str:
25
+ """Refine the story using the Gemini API with two-step refinement"""
26
+ try:
27
+ # First refinement
28
+ first_prompt = f"""
29
+ Refine the following story to make it coherent, grammatically correct, and fluent.
30
+ Maintain the original plot and ending, but enhance readability, fluency, and consistency:
31
+
32
+ {story}
33
+ """
34
+
35
+ first_response = gemini_model.generate_content(first_prompt)
36
+ first_refined = first_response.text if hasattr(first_response, 'text') else str(first_response)
37
+
38
+ # Second refinement for final polishing
39
+ final_prompt = f"""
40
+ Please perform a final polish on this story to ensure perfect coherence,
41
+ flow, and narrative structure while maintaining the essence of the story:
42
+
43
+ {first_refined}
44
+ """
45
+
46
+ final_response = gemini_model.generate_content(final_prompt)
47
+ final_refined = final_response.text if hasattr(final_response, 'text') else str(final_response)
48
+
49
+ return final_refined if final_refined else "No refined story generated."
50
+
51
+ except Exception as e:
52
+ print(f"Error in story refinement: {str(e)}")
53
  return story # Return original story if refinement fails