|
2 | 2 | from flask_cors import CORS |
3 | 3 | from services.topic_service import TopicService |
4 | 4 | from services.ai_service import AITopicProcessor |
| 5 | +import os |
| 6 | +import asyncio |
5 | 7 |
|
6 | 8 | app = Flask(__name__) |
7 | | -CORS(app, resources={ |
8 | | - r"/*": { |
9 | | - "origins": "*", |
10 | | - "methods": ["GET", "POST", "OPTIONS"], |
11 | | - "allow_headers": ["Content-Type", "Authorization"] |
12 | | - } |
13 | | -}) |
| 9 | +CORS( |
| 10 | + app, |
| 11 | + resources={ |
| 12 | + r"/*": { |
| 13 | + "origins": "*", |
| 14 | + "methods": ["GET", "POST", "OPTIONS"], |
| 15 | + "allow_headers": ["Content-Type", "Authorization"], |
| 16 | + } |
| 17 | + }, |
| 18 | +) |
14 | 19 |
|
15 | 20 | topic_service = TopicService() |
16 | 21 | ai_processor = AITopicProcessor() |
17 | 22 |
|
18 | | -@app.route('/api/process-topics', methods=['GET', 'POST']) |
| 23 | + |
| 24 | +@app.route("/api/process-topics", methods=["GET", "POST"]) |
19 | 25 | def process_topics(): |
20 | 26 | try: |
21 | | - if request.method == 'POST': |
| 27 | + if request.method == "POST": |
22 | 28 | data = request.get_json() |
23 | | - search_term = data.get('searchTerm', '') |
| 29 | + search_term = data.get("searchTerm", "") |
24 | 30 | else: |
25 | | - search_term = request.args.get('searchTerm', '') |
| 31 | + search_term = request.args.get("searchTerm", "") |
26 | 32 | result = topic_service.process_topics(search_term) |
27 | 33 | return jsonify(result) |
28 | | - |
| 34 | + |
29 | 35 | except Exception as e: |
30 | | - return jsonify({ |
31 | | - "success": False, |
32 | | - "error": str(e), |
33 | | - "message": "An error occurred while processing the request" |
34 | | - }), 500 |
| 36 | + return jsonify( |
| 37 | + { |
| 38 | + "success": False, |
| 39 | + "error": str(e), |
| 40 | + "message": "An error occurred while processing the request", |
| 41 | + } |
| 42 | + ), 500 |
| 43 | + |
35 | 44 |
|
36 | | -@app.route('/api/ai-process', methods=['GET', 'POST']) |
| 45 | +@app.route("/api/ai-process", methods=["GET", "POST"]) |
37 | 46 | def ai_process(): |
38 | 47 | try: |
39 | | - if request.method == 'POST': |
| 48 | + if request.method == "POST": |
40 | 49 | data = request.get_json() |
41 | | - print(data) |
42 | | - |
| 50 | + # print(data) # Debug log |
| 51 | + |
43 | 52 | # Extract parameters using frontend names |
44 | | - model = data.get('selectedModel', 'gpt-3.5-turbo') |
45 | | - api_token = data.get('apiKey', '') |
46 | | - prompt = data.get('customPrompt', '') |
47 | | - selected_topics = data.get('selectedTopics', []) |
48 | | - |
49 | | - print(f"Selected topics: {selected_topics}") |
50 | | - print(f"Using model: {model}") # Debug log |
51 | | - print(f"Prompt length: {len(prompt)}") # Debug log |
52 | | - |
| 53 | + model = data.get("selectedModel", "gpt-3.5-turbo") |
| 54 | + api_token = data.get("apiKey", "") |
| 55 | + prompt = data.get("customPrompt", "") |
| 56 | + selected_topics = data.get("selectedTopics", []) |
| 57 | + |
| 58 | + # print(f"Selected topics: {selected_topics}") # Debug log |
| 59 | + # print(f"Using model: {model}") # Debug log |
| 60 | + # print(f"Prompt length: {len(prompt)}") # Debug log |
| 61 | + |
53 | 62 | # Use the AI processor to analyze the topics |
54 | | - print("About to call AI processor...") # Debug log |
| 63 | + # print("About to call AI processor...") # Debug log |
55 | 64 | ai_result = ai_processor.process_topics( |
56 | 65 | model=model, |
57 | 66 | api_token=api_token, |
58 | 67 | prompt=prompt, |
59 | | - selected_topics=selected_topics |
| 68 | + selected_topics=selected_topics, |
60 | 69 | ) |
61 | | - print(f"AI processing complete. Result length: {len(str(ai_result))}") # Debug log |
| 70 | + # print(f"AI processing complete. Result length: {len(str(ai_result))}") # Debug log |
| 71 | + |
| 72 | + return jsonify({"success": True, "result": ai_result}) |
| 73 | + |
| 74 | + except Exception as e: |
| 75 | + # print(f"Error occurred: {str(e)}") # Debug log |
| 76 | + return jsonify(["error1", "error2", "error3"]), 500 |
| 77 | + |
| 78 | + |
| 79 | +@app.route("/api/explain-topic", methods=["POST"]) |
| 80 | +def explain_topic(): |
| 81 | + try: |
| 82 | + data = request.get_json() |
62 | 83 |
|
63 | | - return jsonify({ |
64 | | - "success": True, |
65 | | - "result": ai_result |
66 | | - }) |
67 | | - |
| 84 | + topic = data.get("topic", "") |
| 85 | + search_term = data.get("searchTerm", "") |
| 86 | + original_topic = data.get("originalTopic", "") |
| 87 | + api_key = data.get("apiKey", "") |
| 88 | + |
| 89 | + if not topic or not search_term or not original_topic: |
| 90 | + return jsonify( |
| 91 | + { |
| 92 | + "success": False, |
| 93 | + "message": "Missing required parameters: topic, searchTerm, or originalTopic", |
| 94 | + } |
| 95 | + ), 400 |
| 96 | + |
| 97 | + if not api_key: |
| 98 | + return jsonify( |
| 99 | + { |
| 100 | + "success": False, |
| 101 | + "message": "API key is required", |
| 102 | + } |
| 103 | + ), 400 |
| 104 | + |
| 105 | + # Create a prompt for the AI to explain the topic |
| 106 | + prompt = f"""Explain '{topic}' in the context of '{search_term}' following this structure: |
| 107 | +1. If it's an abbreviation, what it stands for and its common meaning in software development |
| 108 | +2. How it's typically defined in technical documentation or Wikipedia |
| 109 | +3. Its specific relevance to {search_term} and why developers use this term in repositories |
| 110 | +Keep it concise but informative (1-2 sentences).""" |
| 111 | + |
| 112 | + try: |
| 113 | + # Create an event loop and run the async function |
| 114 | + loop = asyncio.new_event_loop() |
| 115 | + asyncio.set_event_loop(loop) |
| 116 | + |
| 117 | + # Use Gemini for explanations |
| 118 | + explanation = loop.run_until_complete( |
| 119 | + ai_processor.process_topics( |
| 120 | + model="gemini-1.5-flash", |
| 121 | + api_key=api_key, # Use the API key from the request |
| 122 | + prompt=prompt, |
| 123 | + topics=[topic], |
| 124 | + ) |
| 125 | + ) |
| 126 | + loop.close() |
| 127 | + |
| 128 | + if explanation and len(explanation) > 0: |
| 129 | + return jsonify({"success": True, "explanation": explanation[0]}) |
| 130 | + else: |
| 131 | + return jsonify( |
| 132 | + {"success": False, "message": "Failed to generate explanation"} |
| 133 | + ), 500 |
| 134 | + |
| 135 | + except Exception as ai_error: |
| 136 | + raise ai_error |
| 137 | + |
68 | 138 | except Exception as e: |
69 | | - print(f"Error occurred: {str(e)}") # Debug log |
70 | | - return jsonify(["error1","error2","error3"]), 500 |
| 139 | + return jsonify( |
| 140 | + { |
| 141 | + "success": False, |
| 142 | + "error": str(e), |
| 143 | + "message": "An error occurred while generating the explanation", |
| 144 | + } |
| 145 | + ), 500 |
| 146 | + |
71 | 147 |
|
72 | | -@app.route('/') |
| 148 | +@app.route("/") |
73 | 149 | def home(): |
74 | 150 | return "Hello World!" |
75 | 151 |
|
76 | | -if __name__ == '__main__': |
| 152 | + |
| 153 | +if __name__ == "__main__": |
77 | 154 | print("Starting Flask server...") |
78 | 155 | port = 5002 |
79 | 156 | print(f"Server running on: http://127.0.0.1:{port}") |
80 | | - app.run(host='127.0.0.1', port=port, debug=True) |
| 157 | + app.run(host="127.0.0.1", port=port, debug=True) |
0 commit comments