stephenmccartney1234 commited on
Commit
c924889
·
verified ·
1 Parent(s): 1090ba5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -29
app.py CHANGED
@@ -1,36 +1,50 @@
1
  import gradio as gr
2
- import openai
3
- import sys
4
- import os
5
 
6
- os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
 
 
 
 
7
 
8
- def generate_text(prompt):
9
- # Add custom text to the prompt
10
- input_text = prompt + "Pretend you are an experience builder/insurance expert writer writing about property claims for insurance. Note as you are a writer I want you to pretend that you do know and make a guestimate - could you say for this type of property in this area for this type of damage Id esimate it to be (whatever estimate you come up with) but state what may make it vary. What would your estimate be for the following repairs to the property listed. Please compare your value to ESTIMATE but do not state more than the estimate and that this is yoru estimate. Don’t state that its fictional. Here is the property and damage."
11
- # Generate text using OpenAI API
12
- response = openai.Completion.create(
13
- engine="gpt-3.5-turbo",
14
- prompt=input_text,
15
- max_tokens=1024,
16
- n=1,
17
- stop=None,
18
- temperature=0.7
19
- )
20
 
21
- # Return the generated text
22
- return response.choices[0].text.strip()
23
 
24
- # Define the input and output interfaces using Gradio
25
- input_text = gr.inputs.Textbox(label="Please enter the type of property and the damage done")
26
- output_text = gr.outputs.Textbox(label="Generated text:")
27
 
28
- # Create the Gradio app and run it
29
- gradio_app = gr.Interface(
30
- fn=generate_text,
31
- inputs=input_text,
32
- outputs=output_text,
33
- title="AssessorAI Tool",
34
- description="An AI that helps you validate if repair estimates are acceptable"
 
 
 
 
 
 
 
 
 
 
 
 
35
  )
36
- gradio_app.launch()
 
 
 
 
1
  import gradio as gr
2
+ import requests
 
 
3
 
4
+ def chat_with_pdf(content):
5
+ headers = {
6
+ "x-api-key": "sec_xxxxxx", # Replace with your API key
7
+ "Content-Type": "application/json",
8
+ }
9
 
10
+ data = {
11
+ "stream": True,
12
+ "sourceId": "src_xxxxxx", # Replace with your source ID
13
+ "messages": [
14
+ {
15
+ "role": "user",
16
+ "content": content,
17
+ },
18
+ ],
19
+ }
 
 
20
 
21
+ url = "https://api.chatpdf.com/v1/chats/message"
 
22
 
23
+ try:
24
+ response = requests.post(url, json=data, headers=headers, stream=True)
25
+ response.raise_for_status()
26
 
27
+ if response.iter_content:
28
+ max_chunk_size = 1024
29
+ chat_response = ""
30
+ for chunk in response.iter_content(max_chunk_size):
31
+ text = chunk.decode()
32
+ chat_response += text.strip()
33
+ return chat_response
34
+ else:
35
+ return "No data received"
36
+ except requests.exceptions.RequestException as error:
37
+ return str(error)
38
+
39
+ # Create Gradio interface
40
+ iface = gr.Interface(
41
+ fn=chat_with_pdf,
42
+ inputs=gr.inputs.Textbox(lines=2, placeholder="Ask something..."),
43
+ outputs="text",
44
+ title="ChatPDF Query Interface",
45
+ description="Type your question to get answers from PDFs."
46
  )
47
+
48
+ # Run the Gradio app
49
+ if __name__ == "__main__":
50
+ iface.launch()