Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,44 @@
|
|
1 |
-
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
return response
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from chatbot import chatbot
|
4 |
+
import anthropic
|
5 |
|
6 |
+
# Function to interact with Anthropi API and generate response
|
7 |
+
def get_anthropic_response(question):
|
8 |
+
# Initialize the Anthropi client with your API key
|
9 |
+
client = anthropic.Anthropic(api_key="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")
|
|
|
10 |
|
11 |
+
# Specify parameters for text generation
|
12 |
+
model_name = "claude-3-opus-20240229"
|
13 |
+
max_tokens = 1024
|
14 |
+
|
15 |
+
# Generate response using the specified model and parameters
|
16 |
+
message = client.messages.create(
|
17 |
+
model=model_name,
|
18 |
+
max_tokens=max_tokens,
|
19 |
+
messages=[{"role": "user", "content": question}]
|
20 |
+
)
|
21 |
+
|
22 |
+
# Extract and return the generated response
|
23 |
+
return message.content
|
24 |
+
|
25 |
+
# Function to simulate a chatbot interaction
|
26 |
+
def chatbot(user_message):
|
27 |
+
# Use the Anthropi model to generate a response
|
28 |
+
response = get_anthropic_response(user_message)
|
29 |
+
print(response)
|
30 |
+
return response[0].text # Directly return the generated text
|
31 |
+
|
32 |
+
app = FastAPI()
|
33 |
+
|
34 |
+
# Endpoint to handle chat requests
|
35 |
+
@app.post("/chat", response_model=str)
|
36 |
+
def chat_endpoint(chat_request: str):
|
37 |
+
return chatbot(chat_request)
|
38 |
+
|
39 |
+
def main():
|
40 |
+
# Launch Gradio interface for the chatbot
|
41 |
+
gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Chatbot").launch(share=True)
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
main()
|