Spaces:
Sleeping
Sleeping
Safwanahmad619
commited on
Commit
•
525f438
1
Parent(s):
04bb3cb
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
import anthropic
|
4 |
+
|
5 |
+
with st.sidebar:
|
6 |
+
anthropic_api_key = st.text_input("Anthropic API Key", key="file_qa_api_key", type="password")
|
7 |
+
"[View the source code](https://github.com/streamlit/llm-examples/blob/main/pages/1_File_Q%26A.py)"
|
8 |
+
"[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/streamlit/llm-examples?quickstart=1)"
|
9 |
+
|
10 |
+
st.title("📝 File Q&A with Anthropic")
|
11 |
+
|
12 |
+
uploaded_file = st.file_uploader("Upload an article", type=("txt", "md"))
|
13 |
+
|
14 |
+
question = st.text_input(
|
15 |
+
"Ask something about the article",
|
16 |
+
placeholder="Can you give me a short summary?",
|
17 |
+
disabled=not uploaded_file,
|
18 |
+
)
|
19 |
+
|
20 |
+
if uploaded_file and question and not anthropic_api_key:
|
21 |
+
st.info("Please add your Anthropic API key to continue.")
|
22 |
+
|
23 |
+
if uploaded_file and question and anthropic_api_key:
|
24 |
+
article = uploaded_file.read().decode()
|
25 |
+
prompt = f"""{anthropic.HUMAN_PROMPT} Here's an article:\n\n
|
26 |
+
{article}\n\n\n\n{question}{anthropic.AI_PROMPT}"""
|
27 |
+
|
28 |
+
client = anthropic.Client(api_key=anthropic_api_key)
|
29 |
+
response = client.completions.create(
|
30 |
+
prompt=prompt,
|
31 |
+
stop_sequences=[anthropic.HUMAN_PROMPT],
|
32 |
+
model="claude-2", #"claude-2" for Claude 2 model
|
33 |
+
max_tokens_to_sample=100,
|
34 |
+
)
|
35 |
+
st.write("### Answer")
|
36 |
+
st.write(response.completion)
|