aniyanm commited on
Commit
5474e12
1 Parent(s): 581e962

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +26 -7
api.py CHANGED
@@ -23,28 +23,47 @@ app = FastAPI()
23
 
24
  @app.post("/chat")
25
  async def chat(body: dict = Body(...)):
26
- print(body['prompt'])
 
 
 
 
 
 
 
27
  response = client.chat.completions.create(
28
  model="gpt-3.5-turbo",
29
  messages=[
30
- {"role": "system", "content": "You are a helpful assistant."},
31
  {"role": "user", "content": body['prompt']}
32
  ]
33
  )
34
  return response
35
 
36
 
37
- def chatbot(input, history):
38
- response = requests.post("https://aniyanm-fastapi.hf.space/chat", json={"prompt": input})
39
- return response.json()['choices'][0]['message']['content']
40
 
41
 
42
  # Define the title and description of the interface to greet the user
43
  title = "Welcome to the Chatbot Interface!"
44
  description = "Feel free to start a conversation with the chatbot."
45
 
 
 
 
 
46
  # Launching the Chat
47
- chatapp = gr.ChatInterface(fn=chatbot, title=title, description=description)
 
 
 
 
 
48
 
49
  # Mounting the chatapp to the FastAPI via custom path
50
- app = gr.mount_gradio_app(app, chatapp, path=CUSTOM_PATH)
 
 
 
 
23
 
24
  @app.post("/chat")
25
  async def chat(body: dict = Body(...)):
26
+
27
+ if(body["context"] == "Grammar Checker"):
28
+ content = "You are a grammar expert. Grammar check and suggest with the corrected paragraph. Also calculate a confidence score for the generated content considering the punctuations, correctness, clarity, enganement and delivery"
29
+ elif(body["context"] == "Gift Recommender"):
30
+ content = "You are a Gift Recommender. Suggest Gifts based on the age and gender."
31
+ else:
32
+ content = "You are a helpful assistant."
33
+
34
  response = client.chat.completions.create(
35
  model="gpt-3.5-turbo",
36
  messages=[
37
+ {"role": "system", "content": content+" The output should be in more natural tone."},
38
  {"role": "user", "content": body['prompt']}
39
  ]
40
  )
41
  return response
42
 
43
 
44
+ def converse(inp, history, context):
45
+ response = requests.post("http://localhost:8000/chat", json={"prompt": inp, "context": context})
46
+ return response.json()['choices'][0]['message']['content']
47
 
48
 
49
  # Define the title and description of the interface to greet the user
50
  title = "Welcome to the Chatbot Interface!"
51
  description = "Feel free to start a conversation with the chatbot."
52
 
53
+ dropdown = gr.Dropdown(
54
+ ["Assistant", "Grammar Checker", "Gift Recommender"], label="Context", value="Assistant"
55
+ )
56
+
57
  # Launching the Chat
58
+ chatapp = gr.ChatInterface(
59
+ fn=converse,
60
+ title=title,
61
+ description=description,
62
+ additional_inputs=[dropdown]
63
+ )
64
 
65
  # Mounting the chatapp to the FastAPI via custom path
66
+ app = gr.mount_gradio_app(app, chatapp, path=CUSTOM_PATH)
67
+
68
+ # Run this from the terminal as you would normally start a FastAPI app: `uvicorn run:app`
69
+ # and navigate to http://localhost:8000/gradio in your browser.