saicharantej commited on
Commit
b3faf78
·
1 Parent(s): bc8cc79

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -0
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ # Set up OpenAI API credentials
3
+ import openai
4
+ openai.api_key = os.getenv('api_token')
5
+ import gradio as gr
6
+
7
+ def get_completion(prompt, model="gpt-3.5-turbo"):
8
+ messages = [{"role": "user", "content": prompt}]
9
+ response = openai.ChatCompletion.create(
10
+ model=model,
11
+ messages=messages,
12
+ temperature=0, # this is the degree of randomness of the model's output
13
+ )
14
+ return response.choices[0].message["content"]
15
+
16
+ def generate_thought(question):
17
+ prompt_sum = f"""
18
+ You are an advanced Large Language Model (LLM) with the capabilities to engage in thought experiments and explore complex scientific questions. Your mission is to apply the "Einsteinian Thought Experiment" framework to investigate the following fundamental question:
19
+
20
+ {question}
21
+
22
+ Using your exceptional reasoning abilities and access to well-established scientific principles, embark on a hypothetical journey of inquiry. Develop a compelling thought experiment that effectively addresses the question at hand. Remember to incorporate imaginative and extreme scenarios to isolate key variables and simplify the problem.
23
+
24
+ Analyze the consequences and implications of your hypothetical scenario, visualizing the potential outcomes with respect to existing scientific knowledge. Your goal is to challenge conventional understanding and derive new hypotheses or conjectures that could explain the observed phenomena.
25
+
26
+ Once you have conducted the thought experiment, proceed to discuss your findings and offer insights into how this hypothetical exploration may shed light on the original question. Explain the significance of the results and how they might inform real-world scientific research and investigations.
27
+
28
+ Remember, your ability to think creatively, apply scientific principles, and reason logically is instrumental in unraveling the mysteries of the universe. Go forth and apply the "Einsteinian Thought Experiment" framework to reveal new perspectives and potential breakthroughs!
29
+ """
30
+
31
+ response_sum = get_completion(prompt_sum)
32
+ return response_sum
33
+
34
+ def beautify_thought(thought):
35
+ prompt_sum= f"""
36
+ You've made a captivating observation {thought} that has sparked the reader's curiosity.
37
+ Now, I want to present the thought in a nicely formatted html output. Generate the necessary tags and modify the output so that it shows up to the user with the necessary indentation.
38
+ """
39
+
40
+ response_sum = get_completion(prompt_sum)
41
+ return response_sum
42
+
43
+ def summarize_thought(thought):
44
+ prompt_sum= f"""
45
+ You've made a captivating observation {thought} that has sparked the reader's curiosity.
46
+ Now, take your inquiry to the next level with one search term to unravel the mystery and gain deeper insights into your observation.
47
+ Output:
48
+ search_term
49
+ """
50
+
51
+ response_sum = get_completion(prompt_sum)
52
+ return response_sum
53
+
54
+ import requests
55
+ import urllib
56
+ import pandas as pd
57
+ from requests_html import HTML
58
+ from requests_html import HTMLSession
59
+
60
+ def get_source(url):
61
+ """Return the source code for the provided URL.
62
+
63
+ Args:
64
+ url (string): URL of the page to scrape.
65
+
66
+ Returns:
67
+ response (object): HTTP response object from requests_html.
68
+ """
69
+
70
+ try:
71
+ session = HTMLSession()
72
+ response = session.get(url)
73
+ return response
74
+
75
+ except requests.exceptions.RequestException as e:
76
+ print(e)
77
+
78
+ def get_results(query):
79
+
80
+ query = urllib.parse.quote_plus(query)
81
+ response = get_source("https://www.google.co.uk/search?q=" + query)
82
+
83
+ return response
84
+
85
+ def parse_results(response):
86
+
87
+ css_identifier_result = ".tF2Cxc"
88
+ css_identifier_title = "h3"
89
+ css_identifier_link = ".yuRUbf a"
90
+ css_identifier_text = ".VwiC3b"
91
+
92
+ results = response.html.find(css_identifier_result)
93
+
94
+ output = []
95
+
96
+ for result in results:
97
+
98
+ # Check if the elements were found before accessing their attributes
99
+ title_element = result.find(css_identifier_title, first=True)
100
+ link_element = result.find(css_identifier_link, first=True)
101
+ text_element = result.find(css_identifier_text, first=True)
102
+
103
+ # Skip the current result if any of the required elements is not found
104
+ if not (title_element and link_element and text_element):
105
+ continue
106
+
107
+ item = link_element.attrs['href']
108
+ #'text': text_element.text
109
+
110
+
111
+ output.append(item)
112
+
113
+ return output[:3]
114
+
115
+ def google_search(query):
116
+ response = get_results(query)
117
+ return parse_results(response)
118
+
119
+ def format_output(text_info, urls_list):
120
+ # Format text information
121
+ formatted_text = f"{text_info}"
122
+
123
+ # Format URLs as hyperlinks
124
+ formatted_links = ""
125
+ for url in urls_list:
126
+ formatted_links += f'<a href="{url}" target="_blank">{url}</a><br>'
127
+
128
+ # Combine text information and hyperlinks
129
+ formatted_output = formatted_text + formatted_links
130
+ return formatted_output
131
+
132
+ def thought_experiment(question):
133
+ experiment_result = generate_thought(question)
134
+ beautify_result = beautify_thought(experiment_result)
135
+ thought_topic = summarize_thought(experiment_result)
136
+ results = google_search(thought_topic)
137
+ output = format_output(beautify_result, results)
138
+ return output
139
+
140
+ inputs = [
141
+ gr.inputs.Textbox(label="Enter your question for the thought experiment")
142
+ ]
143
+ bot = gr.Interface(fn=thought_experiment, inputs=inputs, outputs="html", title="Ask Einstein: Perform a thought experiment",
144
+ description="""Ask Einstein is an innovative Gradio app designed to ignite your imagination and stimulate creative thinking. Step into a world of scientific curiosity, where you can explore hypothetical scenarios and inquire about the mysteries of the universe.""",
145
+ article=f"Example thought experiment: Can teleportation of human beings be achieved?")
146
+
147
+ bot.launch()