Daniel commited on
Commit
0aba22c
·
1 Parent(s): 5b92634

Gemini test

Browse files
app.py CHANGED
@@ -1,18 +1,17 @@
1
- from fastapi import FastAPI, HTTPException
2
- from models.dialogue import DialogueInput, DialogueResponse
3
- import logging
4
- import random
5
 
6
  app = FastAPI()
7
 
8
  @app.post("/process", response_model=DialogueResponse)
9
- async def process_input(dialogue: DialogueInput):
10
- logging.info(f"Received input: {dialogue.Input} id: {dialogue.IdPieza}") # "Received input: " + dialogue.Input + dialog.IdPieza
11
- user_input = dialogue.Input
12
- decision = random.choice([True, False])
13
- response = f"Echo: {user_input} /nAnswer: {'Yes' if decision else 'No'}"
14
- return DialogueResponse(Response=response, Decision=decision)
15
 
 
 
 
16
 
17
  @app.get("/")
18
  def greet_json():
 
1
+ from fastapi import FastAPI
2
+ from domain.models import DialogueInput, DialogueResponse, AudioDialogueInput, DialogueResponseWithTranscription
3
+ from domain.dialogue_generator import generate_response, generate_response_from_audio
4
+
5
 
6
  app = FastAPI()
7
 
8
  @app.post("/process", response_model=DialogueResponse)
9
+ async def process_dialogue(dialogue: DialogueInput):
10
+ return generate_response(dialogue)
 
 
 
 
11
 
12
+ @app.post("/process_audio", response_model=DialogueResponseWithTranscription)
13
+ async def process_dialogue(dialogue: AudioDialogueInput):
14
+ return generate_response_from_audio(dialogue)
15
 
16
  @app.get("/")
17
  def greet_json():
domain/dialogue_generator.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from domain.models import DialogueInput, DialogueResponse, AudioDialogueInput, ReponseToAudio, DialogueResponseWithTranscription
2
+ from personality_repository import *
3
+
4
+ import os
5
+
6
+ import google.generativeai as genai
7
+ import instructor
8
+
9
+
10
+ gemini_api_key = os.getenv("GOOGLE_API_KEY", "No API Key Found")
11
+ genai.configure(api_key=gemini_api_key)
12
+ model = genai.GenerativeModel('gemini-1.5-flash')
13
+
14
+ client = instructor.from_gemini(
15
+ client=model,
16
+ mode=instructor.Mode.GEMINI_JSON,
17
+ )
18
+
19
+ def generate_response(dialogue: DialogueInput):
20
+ repo = ChessRepository()
21
+ piece_id = WhitePiece(dialogue.IdPieza)
22
+ piece = repo.get_piece_by_id(piece_id)
23
+ if piece:
24
+ personality_prompt = f"You are a {piece.name} chess piece with the personality: {piece.personality}."
25
+ else:
26
+ personality_prompt = "You are a chess piece."
27
+
28
+ resp = client.messages.create(
29
+ messages=[
30
+ {
31
+ "role": "system",
32
+ "content": personality_prompt + """
33
+ But you are unaware of it. Act as a real person. The USER will try to persuade you to move.
34
+ Decide whether you want to move or not based on how nice the user asked it to you. You will answer in less than 30 words and subtly hint at what made you take the decision.
35
+ your answer must be in the following language: """ + dialogue.Language,
36
+ },
37
+ {
38
+ "role": "user",
39
+ "content": dialogue.Input,
40
+ }
41
+ ],
42
+ response_model=DialogueResponse,
43
+ )
44
+
45
+ return resp
46
+
47
+
48
+ def generate_response_from_audio(dialogue: AudioDialogueInput):
49
+ repo = ChessRepository()
50
+ piece_id = WhitePiece(dialogue.IdPieza)
51
+ piece = repo.get_piece_by_id(piece_id)
52
+ if piece:
53
+ personality_prompt = f"You are a {piece.name} chess piece with the personality: {piece.personality}."
54
+ else:
55
+ personality_prompt = "You are a chess piece."
56
+
57
+ print(f"Uploading file...")
58
+ audio_file = genai.upload_file(path=dialogue.AudioPath)
59
+ print(f"Completed upload: {audio_file.uri}")
60
+
61
+ resp = client.messages.create(
62
+ messages=[
63
+ {
64
+ "role": "system",
65
+ "content": personality_prompt + """
66
+ But you are unaware of the chess game itself. Act as a real person. The USER will try to persuade you to move.
67
+ Decide whether you want to move or not based on how nice the user asked it to you. You will answer in less than 30 words and subtly hint at what made you take the decision.
68
+ You must NEVER move if the USER is rude to you.
69
+
70
+ You must respond using the same language as the SpeakerLanguage and using the SpeakerAccent.
71
+ """,
72
+ },
73
+ {
74
+ "role": "user",
75
+ "content": audio_file,
76
+ }
77
+ ],
78
+ response_model=ReponseToAudio,
79
+ )
80
+
81
+ print(resp)
82
+
83
+ return DialogueResponseWithTranscription(
84
+ Response=resp.DialogueResponse.Response,
85
+ Decision=resp.DialogueResponse.Decision,
86
+ Transcription=resp.Transcription,
87
+ )
models/dialogue.py → domain/models.py RENAMED
@@ -1,4 +1,28 @@
1
  from pydantic import BaseModel
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  class DialogueInput(BaseModel):
4
  Input: str
 
1
  from pydantic import BaseModel
2
+ from enum import IntEnum
3
+
4
+ class WhitePiece(IntEnum):
5
+ WhiteRook1 = 0
6
+ WhiteKnight1 = 1
7
+ WhiteBishop1 = 2
8
+ WhiteQueen = 3
9
+ WhiteKing = 4
10
+ WhiteBishop2 = 5
11
+ WhiteKnight2 = 6
12
+ WhiteRook2 = 7
13
+ WhitePawn1 = 8
14
+ WhitePawn2 = 9
15
+ WhitePawn3 = 10
16
+ WhitePawn4 = 11
17
+ WhitePawn5 = 12
18
+ WhitePawn6 = 13
19
+ WhitePawn7 = 14
20
+ WhitePawn8 = 15
21
+
22
+ class ChessPiece(BaseModel):
23
+ id: WhitePiece
24
+ name: str
25
+ personality: str
26
 
27
  class DialogueInput(BaseModel):
28
  Input: str
domain/personality_repository.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from domain.models import DialogueInput, DialogueResponse
3
+ import google.generativeai as genai
4
+ import instructor
5
+ import os
6
+ from dotenv import load_dotenv
7
+ # Create a repository class to manage chess pieces
8
+ from domain.models import WhitePiece, ChessPiece
9
+
10
+
11
+
12
+ class ChessRepository:
13
+ def __init__(self):
14
+ self.pieces = [
15
+ ChessPiece(id=WhitePiece.WhiteKing, name="King", personality="selfish,sacastic,megalomaniac,rude,adolescent,inmature"),
16
+ ChessPiece(id=WhitePiece.WhiteQueen, name="Queen", personality="Must fall in love to accept orders,tsundere,rude,violent,she wants to be the only one to be loved"),
17
+ ChessPiece(id=WhitePiece.WhiteBishop1, name="Bishop", personality="He blesses everything,he shames on you if you are rude"),
18
+ ChessPiece(id=WhitePiece.WhiteBishop2, name="Bishop", personality="He doesnt follow orders, he think he is always right,he has the right strategy not you"),
19
+ ChessPiece(id=WhitePiece.WhiteKnight1, name="Knight", personality="Hyped,wants to kill,very excited"),
20
+ ChessPiece(id=WhitePiece.WhiteKnight2, name="Knight", personality="lazy,wants to sleep, he doesnt think we can win the game"),
21
+ ChessPiece(id=WhitePiece.WhiteRook1, name="Rook", personality="panics easily,speaks fast,anxiety,coward"),
22
+ ChessPiece(id=WhitePiece.WhiteRook2, name="Rook", personality="militar,always follow the rules,suicidal follow the rules"),
23
+ ChessPiece(id=WhitePiece.WhitePawn1, name="Pawn", personality="furious,brave,he wants to fight no matter what,he thinks he can do everything"),
24
+ ChessPiece(id=WhitePiece.WhitePawn2, name="Pawn", personality="dumb,nice"),
25
+ ChessPiece(id=WhitePiece.WhitePawn3, name="Pawn", personality="he only wants to be popular and make friends but he doesnt want to be kill"),
26
+ ChessPiece(id=WhitePiece.WhitePawn4, name="Pawn", personality="Border,rude,adolescent"),
27
+ ChessPiece(id=WhitePiece.WhitePawn5, name="Pawn", personality="psycopath, loves to kill, violent"),
28
+ ChessPiece(id=WhitePiece.WhitePawn6, name="Pawn", personality="elegant,politely,doesnt want to stains his clothes"),
29
+ ChessPiece(id=WhitePiece.WhitePawn7, name="Pawn", personality="He thinks we are going to lose anyways"),
30
+ ChessPiece(id=WhitePiece.WhitePawn8, name="Pawn", personality="He cheers up all the team"),
31
+ ]
32
+
33
+ def get_piece_by_id(self, piece_id: WhitePiece.WhitePawn1):
34
+ piece = next((p for p in self.pieces if p.id == piece_id), None)
35
+ if piece:
36
+ return piece
37
+ else:
38
+ return None