Vitrous commited on
Commit
ec357c2
·
verified ·
1 Parent(s): d95eb39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -50
app.py CHANGED
@@ -51,43 +51,6 @@ def load_model_norm():
51
 
52
  return model, tokenizer
53
 
54
-
55
- @app.get("/", tags=["Home"])
56
- async def api_home():
57
- return {'detail': 'Welcome to Eren Bot!'}
58
-
59
-
60
- # Endpoint to start a new conversation thread
61
- @app.post('/api/start_conversation')
62
- async def start_conversation(request: Request):
63
- data = await request.json()
64
- prompt = data.get('prompt')
65
-
66
- # Generate a response for the initial prompt
67
- response = generate_response(prompt)
68
-
69
- # Create a new conversation thread and store the prompt and response
70
- thread_id = len(conversations) + 1
71
- conversations[thread_id] = {'prompt': prompt, 'responses': [response]}
72
-
73
- return {'thread_id': thread_id, 'response': response}
74
-
75
-
76
- # Endpoint to get the response of a conversation thread
77
- @app.get('/api/get_response/{thread_id}')
78
- async def get_response(thread_id: int):
79
- if thread_id not in conversations:
80
- raise HTTPException(status_code=404, detail="Thread not found")
81
-
82
- # Retrieve the conversation thread
83
- thread = conversations[thread_id]
84
-
85
- # Get the latest response in the conversation
86
- response = thread['responses'][-1]
87
-
88
- return {'response': response}
89
-
90
-
91
  # Function to generate a response using the model
92
  def generate_response(prompt: str) -> str:
93
  PERSONA_NAME = "Ivana"
@@ -126,19 +89,49 @@ def generate_response(prompt: str) -> str:
126
  return generated_text
127
 
128
 
129
- def main():
130
- print("Welcome to the Chatbot!")
131
- print("Type 'exit' to end the conversation.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- while True:
134
- user_input = input("You: ")
135
-
136
- if user_input.lower() == 'exit':
137
- print("Goodbye!")
138
- break
139
-
140
- response = generate_response(user_input)
141
- print("Chatbot:", response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
- main()
144
 
 
51
 
52
  return model, tokenizer
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  # Function to generate a response using the model
55
  def generate_response(prompt: str) -> str:
56
  PERSONA_NAME = "Ivana"
 
89
  return generated_text
90
 
91
 
92
+ @app.get("/", tags=["Home"])
93
+ async def api_home():
94
+ return {'detail': 'Welcome to Eren Bot!'}
95
+
96
+
97
+ # Endpoint to start a new conversation thread
98
+ @app.post('/api/start_conversation')
99
+ async def start_conversation(request: Request):
100
+ data = await request.json()
101
+ prompt = data.get('prompt')
102
+
103
+ # Generate a response for the initial prompt
104
+ response = generate_response(prompt)
105
+
106
+ # Create a new conversation thread and store the prompt and response
107
+ thread_id = len(conversations) + 1
108
+ conversations[thread_id] = {'prompt': prompt, 'responses': [response]}
109
+
110
+ return {'thread_id': thread_id, 'response': response}
111
+
112
 
113
+ # Endpoint to get the response of a conversation thread
114
+ @app.get('/api/get_response/{thread_id}')
115
+ async def get_response(thread_id: int):
116
+ if thread_id not in conversations:
117
+ raise HTTPException(status_code=404, detail="Thread not found")
118
+
119
+ # Retrieve the conversation thread
120
+ thread = conversations[thread_id]
121
+
122
+ # Get the latest response in the conversation
123
+ response = thread['responses'][-1]
124
+
125
+ return {'response': response}
126
+
127
+ @app.post('/api/chat')
128
+ async def chat(request: Request, chat_input: ChatInput):
129
+ prompt = chat_input.prompt
130
+
131
+ # Generate a response based on the prompt
132
+ response = generate_response(prompt)
133
+
134
+ return {"response": response}
135
+
136
 
 
137