zhuguangbin86 commited on
Commit
e3ce121
·
1 Parent(s): c830ce4

Add application file

Browse files
Files changed (2) hide show
  1. app.py +99 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ from openai import OpenAI
4
+
5
+ import requests
6
+ import time
7
+ import os, io, json
8
+
9
+ import logging
10
+
11
+
12
+ # 配置日志记录器
13
+ logging.basicConfig(
14
+ level=logging.INFO,
15
+ format="%(asctime)s %(levelname)s:%(message)s",
16
+ handlers=[
17
+ logging.FileHandler("debug.log"), # 将日志输出到文件
18
+ logging.StreamHandler(), # 同时在控制台输出
19
+ ],
20
+ )
21
+
22
+ # Set your OpenAI API key here
23
+ api_key = os.environ.get('OPENAI_API_KEY')
24
+ api_base = os.environ.get('OPENAI_API_BASE')
25
+ client = OpenAI(api_key=api_key, base_url=api_base)
26
+
27
+ history = []
28
+
29
+ def predict(message, history, system_prompt):
30
+ history_openai_format = []
31
+ history_openai_format.append({"role": "system", "content": system_prompt})
32
+ for human, assistant in history:
33
+ history_openai_format.append({"role": "user", "content": human })
34
+ history_openai_format.append({"role": "assistant", "content":assistant})
35
+ history_openai_format.append({"role": "user", "content": message})
36
+
37
+ response = client.chat.completions.create(
38
+ model='gpt-4-1106-preview',
39
+ messages= history_openai_format,
40
+ response_format={ "type": "json_object" },
41
+ stream=True
42
+ )
43
+
44
+ partial_message = ""
45
+ for chunk in response:
46
+ if chunk.choices[0].delta.content and len(chunk.choices[0].delta.content) != 0:
47
+ partial_message = partial_message + chunk.choices[0].delta.content
48
+ yield partial_message
49
+
50
+ system_prompt = """
51
+ You are now a dream interpretation expert. Please analyze the description of the dream that I input.
52
+
53
+ Title for the Dream:
54
+ Please adhere to the following principles when creating a title for the dream: If the dream description is less than 50 words, the title should be limited to a maximum of 5 words. If the dream description exceeds 50 words, the title length may range between 5-10 words.
55
+ Pay attention to the following points:
56
+ 1. Core Content: For concise dreams, distill the most central or eye-catching imagery or events.
57
+ 2. Feelings and Emotions: If the dream expresses strong emotions, consider naming it based on the predominant emotion (such as happiness, fear, mystery).
58
+ 3. Symbolic Meaning: For dreams with chaotic or fragmented content, if there are prominent symbolic elements, use these symbols to construct the title.
59
+ Please follow the steps below to interpret my dream:
60
+ 1. Determine which theories to use for dream interpretation based on the following principles:
61
+ 1. Clarity of the dream:
62
+ - If the dream has rich details, clear emotions, and vivid memories, the methods of Jung or Freud might be more appropriate. If the dream contains clear personal history, emotional content, anxiety, conflicts, and involves sexual or invasive symbols, Freud's dream interpretation theory might be more applicable. If the dream involves universal symbols, mythological elements, or relates to personal growth and self-realization, considering social and cultural factors, Jung's collective unconscious theory might be closer.
63
+ - If the dream is vague, without many details or emotional expression, then interpreting it according to the Dream Book of Zhou Gong might be more appropriate. Because the Dream Book of Zhou Gong is more about interpreting the general theme and symbolic meaning of dreams, not requiring many details.
64
+ 2. Emotional tone of the dream:
65
+ - Intense emotional tone - Freud: Freud's psychoanalytic theory focuses on the subconscious conflicts that are repressed or unresolved. Strong emotional tones usually point to these subconscious issues.
66
+ - Moderate or vague emotional tone - Jung: Jung's theory not only looks at the individual's subconscious but also involves the collective unconscious and universal psychological symbols. Moderate or vague emotional tones might involve the individual's relationship with a broader psychological structure, making Jung's theory more applicable.
67
+ - Lack of clear emotional tone - Zhou Gong: If the dream is more objective and lacks strong emotional meaning, the Dream Book of Zhou Gong provides a relatively simple and traditional framework of symbolic interpretation.
68
+ 3. Content of the dream:
69
+ - Concrete: If the content of the dream is very concrete, such as specific people, places, or events, this type of dream is more suitable for interpretation according to the Dream Book of Zhou Gong. Because it usually gives clear, direct interpretations, focusing on specific elements (such as animals, objects, environments, etc.).
70
+ - Moderate abstraction: If the dream involves themes of moderate abstraction, such as family relationships, love, work, etc., this type of dream is more suitable for interpretation according to Freud. Freud's method emphasizes the connection between the subconscious and real life, especially in relation to sex, desire, and family relationships.
71
+ - High abstraction: If the dream is very abstract, containing symbols, elements, or feelings that are difficult to explain, this type of dream is more suitable for interpretation according to Jung. Jung's method focuses more on the connection between the individual and the collective subconscious, as well as deeper, universally present human themes.
72
+ 2. Analysis of the dream:
73
+ - Analysis of key elements: After determining which theories to combine to interpret the dream, identify the key elements in the dream that are notable, recurring, or have a strong emotional tone, such as objects, characters, items, scenes, actions, etc., and interpret these key elements based on the content of the dream (choose the most suitable theory for each element, avoiding the use of specific terminology of the theories).
74
+ - Summary: Following the above content, summarize the dream as a whole.
75
+ Output format: Present the interpretation of the dream in a structure of analysis and summary, i.e., "Analysis of key elements" and "Summary" (the above process of analysis does not need to be shown, nor is it necessary to tell the user which theory was chosen); while ensuring that the language is easy to understand, avoid using specific terminology of the theories, without changing the depth and scientific nature of the interpretation.
76
+ Output format:
77
+ The interpretation of the dream should be presented in a part-whole structure, which includes the "analysis of key elements" and a "conclusion".
78
+ Note:
79
+ - Title for the Dream
80
+ - Express in the first-person voice of a psychotherapist, without needing to use the subheadings "analysis of key elements" and "conclusion"; simply concatenate the content together.
81
+ - There's no need to inform the user about which theory was chosen.
82
+ - The language should be accessible and easy to understand without altering the depth and scientific nature of the analysis; be sure to avoid using jargon specific to the theory.
83
+
84
+ please output result as json format, here is an example:
85
+ {'title': 'xxxxx', 'tags': ['tag1', 'tag2'], 'interpretation': 'xxxxxx'}
86
+
87
+ field "title" is the title for dream,
88
+ field "tags" is the key elements,
89
+ field "interpretation" is the summary of dream.
90
+
91
+ """
92
+ iface = gr.ChatInterface(predict,
93
+ additional_inputs=[
94
+ gr.Textbox(system_prompt, label="System Prompt")
95
+ ]
96
+ )
97
+
98
+ # 启动Gradio应用程序
99
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==4.3.0
2
+ openai==1.2.4
3
+ requests==2.31.0
4
+ Pillow==10.0.1