Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
return state, state
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
state,
|
| 16 |
-
],
|
| 17 |
-
) as chatbot:
|
| 18 |
-
if __name__ == "__main__":
|
| 19 |
-
chatbot.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
import uvicorn
|
| 5 |
|
| 6 |
+
openai.api_key = "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"
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
conversation_history = []
|
| 10 |
+
|
| 11 |
+
@app.post("/chat")
|
| 12 |
+
async def chat(request: Request):
|
| 13 |
+
global conversation_history
|
| 14 |
+
message = await request.json()
|
| 15 |
+
|
| 16 |
+
# Convert the user's message to JSON
|
| 17 |
+
message_json = json.dumps(message)
|
| 18 |
+
conversation_history.append({"role": "user", "content": message_json})
|
| 19 |
+
|
| 20 |
+
# Prepare the messages for the GPT-3 API call
|
| 21 |
+
messages = conversation_history.copy()
|
| 22 |
+
# Add a system message to instruct the model
|
| 23 |
+
messages.insert(0, {"role": "system", "content": "You are a story writer. What ever the prompt you always write a story"})
|
| 24 |
+
|
| 25 |
+
completion = openai.ChatCompletion.create(
|
| 26 |
+
model="gpt-4",
|
| 27 |
+
messages=messages
|
| 28 |
+
)
|
| 29 |
+
reply = completion.choices[0].message.content
|
| 30 |
+
|
| 31 |
+
# Append the model's reply to the conversation history
|
| 32 |
+
conversation_history.append({"role": "assistant", "content": reply})
|
| 33 |
+
|
| 34 |
+
return reply
|
| 35 |
+
|
| 36 |
+
def gradio_chat(message, state):
|
| 37 |
+
reply = chat(message)
|
| 38 |
+
state = state + [(message, reply)]
|
| 39 |
return state, state
|
| 40 |
|
| 41 |
+
with gr.ChatInterface(fn=gradio_chat, state_init=[], state=None) as chatbot:
|
| 42 |
+
chatbot.launch(server_name="0.0.0.0", server_port=7860)
|
| 43 |
+
|
| 44 |
+
# Run the FastAPI app
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|