seawolf2357 commited on
Commit
536aa54
1 Parent(s): 56c10f0

initial commit

Browse files
Files changed (1) hide show
  1. app.py +100 -4
app.py CHANGED
@@ -1,7 +1,103 @@
 
1
  import gradio as gr
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
  import gradio as gr
3
+ import os
4
+ import openai
5
+ import newspaper
6
+ import json
7
+ import re
8
+ from transformers import GPT2Tokenizer
9
 
 
 
10
 
11
+ def flip_text(name):
12
+ return "Hello " + name + "!"
13
+ demo = gr.Interface(fn=flip_text, inputs="text", outputs="text")
14
+
15
+
16
+
17
+ # define the text summarizer function
18
+ def flip_text(request, page_url, contraseña, temp):
19
+ try:
20
+ page = newspaper.Article(url=page_url)
21
+ page.download()
22
+ page.parse()
23
+ except Exception as e:
24
+ return "", f"--- An error occurred while processing the URL: {e} ---", ""
25
+
26
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
27
+
28
+ tokens = tokenizer.tokenize(page.text)
29
+ num_tokens = len(tokens)
30
+
31
+ if num_tokens > 10 and num_tokens < 2000:
32
+ openai.api_key = contraseña
33
+ # get the response from openai API
34
+ try:
35
+ response = openai.Completion.create(
36
+ engine="text-davinci-003",
37
+ prompt=request + "\n\n" + page.text,
38
+ max_tokens=2048,
39
+ temperature=temp,
40
+ top_p=0.9,
41
+ )
42
+ # get the response text
43
+ response_text = response.choices[0].text
44
+ # clean the response text
45
+ response_text = re.sub(r'\s+', ' ', response_text)
46
+ return page.text, response_text, num_tokens
47
+ except Exception as e:
48
+ return page.text, f"--- An error occurred while processing the request: {e} ---", num_tokens
49
+ return page.text, "--- Max number of tokens ---", num_tokens
50
+
51
+ # define the gradio interface
52
+ iface = gr.Interface(
53
+ fn=flip_text,
54
+ inputs=[gr.Textbox(lines=1, placeholder="Enter your prompt here...", label="Prompt:", type="text"),
55
+ gr.Textbox(lines=1, placeholder="Enter the URL here...", label="URL to parse:", type="text"),
56
+ gr.Textbox(lines=1, placeholder="Enter your API-key here...", label="API-Key:", type="password"),
57
+ gr.Slider(0.0,1.0, value=0.3, label="Temperature:")
58
+ ],
59
+ outputs=[gr.Textbox(label="Input:"), gr.Textbox(label="Output:"), gr.Textbox(label="Tokens:")],
60
+ examples=[["Summarize the following text as a list:","https://blog.google/outreach-initiatives/google-org/our-commitment-on-using-ai-to-accelerate-progress-on-global-development-goals/","",0.3],
61
+ ["Generate a summary of the following text. Give me an overview of main business impact from the text following this template:\n- Summary:\n- Business Impact:\n- Companies:", "https://ai.googleblog.com/2019/10/quantum-supremacy-using-programmable.html","",0.7]
62
+ ],
63
+ title="Information Extraction with ChatGPT Interface:",
64
+ # description="This tool allows querying the text retrieved from the URL using OpenAI's [text-davinci-003] engine.\nThe URL text can be referenced in the prompt as \"following text\".\nA GPT2 tokenizer is included to ensure that the 2000 token limit for OpenAI queries is not exceeded. Provide a prompt with your request, the url for text retrieval, your api-key and temperature to process the text."
65
+ )
66
+
67
+
68
+
69
+ error_message = ""
70
+
71
+ try:
72
+ iface.launch()
73
+ except Exception as e:
74
+ error_message = "An error occurred: " + str(e)
75
+ iface.outputs[1].value = error_message
76
+
77
+
78
+
79
+
80
+ def flip_image(x):
81
+ return np.fliplr(x)
82
+
83
+
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown("Flip text or image files using this demo.")
86
+ with gr.Tab("Flip Text"):
87
+ text_input = gr.Textbox()
88
+ text_output = gr.Textbox()
89
+ text_button = gr.Button("Flip")
90
+ with gr.Tab("Flip Image"):
91
+ with gr.Row():
92
+ image_input = gr.Image()
93
+ image_output = gr.Image()
94
+ image_button = gr.Button("Flip")
95
+
96
+ with gr.Accordion("Open for More!"):
97
+ gr.Markdown("Look at me...")
98
+
99
+ text_button.click(flip_text, inputs=text_input, outputs=text_output)
100
+ image_button.click(flip_image, inputs=image_input, outputs=image_output)
101
+
102
+ if __name__ == "__main__":
103
+ demo.launch()